Welcome to this review of MVC, which is the 4th 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
Part 5 – MVC and AJAX
Part 6 – Web Pages
Part 7 – Web API
Part 8 – SignalR
MVC stands for Model View Controller.
The Model holds the data, the View represents the User Interface, and the Controller applies the model to the view.
Jesse gives a brief history of ASP.NET MVC starting with Scott Guthrie‘s sketch while he was on a flight to the east coast of the USA, and ending with the release of MVC 5 in 2013.
Jesse introduces the concept of Convention over Configuration, which is similar to the ideas from Ruby on Rails:
- Controllers names end with Controller and live in a folder called Controllers
- Views are named to match the Controller and live in a folder called Views
- Each view is named for an Action in the controller e.g. Views/Product/Edit.cshtml
Out of the box when we create an MVC project we get:
- Adaptive Sizing
Out of The Box
A demo of MVC 5 in Visual Studio 2013. We see the Home, About an Contact page links as well as Login and Register.
We also see the adaptive resizing that gets bundled in with MVC projects by default.
The Controller tells the view what to display. It can directly control the HTML to display, but this is generally a poor practice.
We should use a helper such as HTMLEncode when displaying data from the user.
HTMLEncode helps to protect our site from attack. Jesse uses the HttpUtility method HTMLEncode, but you might want to use AntiXSS in your applications. I recommend reading this Stack Overflow question for details.
Jesse adds an empty MVC5 HelloController.
This contains one Action named Index. Jesse changes it to return a string instead of a view, and adds a 2nd Action named HelloWorld.
We see the relationship between Controller and Action names, and the URL paths that invoke them.
We also see how we can use query string parameters to pass in data to our action methods, and see how we can use HTMLEncode.
There are two ways for Controllers to pass data to the view. We can use ViewBags, or type safe objects.
Jesse discusses the Razor syntax that was introduced in MVC 3, and now available for Web Forms as well. Not mentioned here is the fact that you are not obligated to use Razor – it is the default, but you can change it to another view engine.
We see Razor and Web Forms syntax side by side, and it is easy to note how much uglier the old Web Forms syntax is than the newer and cleaner Razor syntax.
This demo discusses many aspects of Views, including Shared Views.
We see @RenderBody() which is where our view is rendered.
Jesse modifies the default view to create a Books view.
Views should never manipulate data directly. This should usually be done in the controller.
The model represents the data, and it can be manipulated by the controller.
A controller can pass the model to the view, and the view can use the model for type-safe display.
Jesse demonstrates these concepts in a new project called BookDemo.
We also use Entity Framework Code First, and the scaffolding MVC5 Controller with read/write actions and views using Entity Framework.
This is a rapid way to develop, because we just pick our model class from a drop down list, use the wizard to create our data context class for us, and get it to generate views and a layout page as well.
We see the new views Create, Delete, Edit and Index, and the model BookDBContext.
Jesse demonstrates one of the Data Annotations, Display, to set the name of the PubDate property to “Pub Date”.
(This is short for Publication Date, no beer served here sorry!)
We also see the Data Type Attributes, and Jesse gives the Pub Date property the type Date, which removes the times from the display.
Then Jesse describes the Html Helper methods used in Edit.cshtml, such as:
We see that all of this gets translated into standard HTML when we run our program.
We have now covered many, perhaps even most of the basics of the ASP.NET MVC framework.
The next module of the course, and the next episode in this series, looks at some of the more advanced aspects.