One ASP.NET From Scratch: Advanced MVC

jesse-liberty-v2

Welcome to this review of Advanced MVC, which is the 5th module of the Pluralsight course One ASP.NET From Scratch.

This is an easy introduction to ASP.NET MVC by Jesse Liberty.

Jesse has over three decades of experience writing and delivering software projects, is the author of 2 dozen books, and has been a Senior Technical Evangelist for Microsoft and a Distinguished Software Engineer for AT&T.

He is a Xamarin Certified Mobile Developer and MVP, Microsoft MVP and Telerik MVP.

Jesse also the host of “Yet Another Podcast”, which is a great show.

Also in this series:

Part 1 – One ASP.NET From Scratch

Part 2 – Advanced Web Forms

Part 3 – MVC

Part 4 – Advanced MVC

Advanced MVC

Database Management

Jesse begins by talking not about ASP.NET MVC, but Entity Framework. Although these two technologies are very different, it is common for ASP.NET MVC to be used with Entity Framework.

Jesse discusses resetting the Database each time it changes, and seeding the database with starter values.

In the demo, we create a new model which inherits DropCreateDatabaseAlways, and override the Seed method with our own seed data: a couple of Jesse’s classic books plus Pro Windows 8 With XAML and C#.

We also see how to Initialize our database with this data in our Global.asax.

If you are interested in learning Migrations in more depth see Julie Lerman’s course Entity Framework Code First Migrations, or the penultimate module of Simon Hughes’ Code First Entity Framework with Legacy Databases.

Searching

All Forms have an action and a method:

  • Action – where to send the form
  • Method – POST or GET

We should use GET when we are not changing the object, and it should be idempotent.

When we want to update the object, we should use POST.

We begin the demonstration by adding a searchString parameter to out BookController’s Index Action method.

This demonstration uses substantial amounts of LINQ code. By the end of it, we have the ability to select books by Genre using a dropdown list, and the ability to add part of the book’s name into a textbox.

When we click on the Filter button, it filters the lists of books according to our name and genre.

Helpers

We saw some of these in the previous module. Jesse describes the common HTML Helpers by category: Inputs, Strongly Type, Templated Helpers.

Other handy helpers are:

  • Html.Hidden
  • Html.Password
  • Html.RadioButton
  • Html.RadioButtonFor
  • Html.CheckBox
  • Html.ActionLink

Validation

We want to specify our validation in just our place: the model class, and have it applied across all of our views.

We see this in our Book Demo application, adding annotations such as:

  • [Required]
  • [StringLength(60, MinimumLength=3, ErrorMessage=”Must be 3 to 60 chars”)]
  • [Range(1, 100)]

We see that the validation is done client side, and is unobtrusive.

Part 5 – MVC and AJAX is coming soon

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s