The internet is filled up with video tutorials like “Creating wiki using ASP.NET MVC in 15 minutes”. The problem of these tutorials is that they do not cover real life scenarios. They tend to use ViewData and ViewBag to transfer data into View. They use non-typed Html.TextBox() helper methods for generating forms. They use controller method parameters to transfer form data back to the controller one by one. Or, more worse, they directly put ORM entity into controller method parameters, which can lead to huge problems when practiced by a non-experienced developer.

So, such “rapid” approaches can be attractive for the person, whose job is to create such video tutorials (probably trying to compete with the famous RoR one).

But in a real life applications, you’ll probably want to use strongly-typed models,  strongly-typed model-aware Html.TextBoxFor(m => ..) helpers for generating forms, and get strongly-typed POST data into controller method. In other words, you need acceptable maintainability for your projects in MVC.

So here are the rules to remember, if you’re going to develop with real MVC.

  1. Views to ViewModels is a one-to-one relationship. Define separate ViewModel class for every single View.
  2. View dictates ViewModel structure, not the controller.
  3. ViewModles are dummy DTOs, with no logic in them.
  4. Forget about calling Request.IsAuthenticated and similar in your views. ViewModel should contain ALL the data required to render the view.

In my next post I’ll describe how you can use  strongly-typed models in generic view layouts (master pages in MVC2). See Part 2.