Tuesday, December 23, 2014

Using ASP.NET MVC Scaffolding with ViewModels

If you read about ASP.NET MVC Scaffolding, most examples will make references to using Entity Framework based Model classes in order to build out your Views, however, if you are using a layered architecture or a Repository pattern to segregate your data access logic from your Controllers, then you will probably want to know how to leverage Scaffolding in order to bind to your ViewModels instead.

Well, fortunately, Scaffolding only changes slightly when working with ViewModels:

  1. Create your ViewModel class in your project.  This should be wired up with all of the necessary attributes (System.ComponentModel.DataAnnotations) that you will need for your ModelState validation in your Controller methods/actions.  For a complete list of DataAnnotation attributes, you can consult this MSDN documentation: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations%28v=vs.110%29.aspx
  2. Compile your Project/Solution.  If you do not do this first, then the newly created ViewModel class will not appear as part of your Scaffolding options.
  3. In this next step, you have to be careful about which option you choose.  If you choose "Add-->New Scaffolded Item" then you will only get options to add an empty MVC Controller.  Therefore, you should go to the Controllers directory and "Add-->Controller".  You will then be able to choose a Controller with read/write actions.
  4. Once you have created your MVC Controller, you will see a Controller with empty operations for the common actions needed in an MVC Controller.  Of course, none of the Views have actually been added to the project yet.
  5. Right click on one of the relevant MVC Controller actions and click on "Add-->View".  
  6. You will then be prompted to create a View corresponding to the appropriate Controller action and choose the respective Model/ViewModel that you will be using for binding as well as if there is any corresponding Layout page for that View.
  7. Once the View is created, you will notice that it shells out all of the necessary HTML Helpers for the View that you can use as a starting point for modifying your View to meet your requirements.  Of course, if you want to make your Scaffolding even more useful, then you can create custom Display and Editor templates to ensure your Views more closely match your business needs/requirements as described here: http://www.codeguru.com/csharp/.net/net_asp/mvc/using-display-templates-and-editor-templates-in-asp.net-mvc.htm
  8. Once you have decided on the ViewModel classes that you will use for your Controller and Views, you will want to change the generic FormCollection collection controller parameters on your Create, Edit and Delete MVC Controller methods to match the name of your ViewModel class (ex: MVCSampleModel model)
  9. Now you can proceed to create Views for each of your corresponding Controller actions and then fill in the necessary logic in your Controller actions to store and persist your ViewModel classes to your backing data store knowing that all of your MVC Views are already strongly typed and have much of the validation logic already built-in!!








No comments:

Post a Comment