From the course: ASP.NET MVC: Building for Productivity and Maintainability

Unlock the full course today

Join today to access over 22,700 courses taught by industry experts or purchase this course individually.

Updating a view to use a view model

Updating a view to use a view model

From the course: ASP.NET MVC: Building for Productivity and Maintainability

Start my 1-month free trial

Updating a view to use a view model

- [Instructor] At this point in the chapter, I've created a new class to serve as a ViewModel and populated it with data in the controller. So now it's time to switch back to the view and start rewiring it up to those new properties. First I'll change the view's model type to the type of the new ViewModel by updating the model directive like this. Then I'll just start deleting code and replacing it with simple references to the ViewModel's properties. I'll start with the foreach loop on line 14 which iterates through all the products. This should now point to the model's products property rather than the model directly. Note once you change the model type of a razor page you may need to reload it in order for the change to take effect. Now I'll simply delete line 16 and 17. And line seven which pulls the array of ratings from the ViewData object that's no longer used. Then I'll replace any broken references to the discount and rating variables with their new ViewModel properties. The…

Contents