In the previous example, we used the ViewData
dictionary to pass data from the controller to the view. When developing the view, each dictionary item we want to display should be cast to the correct class, resulting in a less maintainable situation. It might also lead to code spaghetti in the view. It would be useful if the ViewData dictionary already knew which class type each of its items represented. This is where the model comes in handy! We are serving employee information to the view, so why not use the Employee
class that we'd previously created as a "the" model for our view? Note that we'd already placed the Employee
class inside the Model folder, which is the appropriate location for model classes.
Views can be made strong-typed by updating the view and replacing the base class of the page (System.Web.Mvc.ViewPage
) with a generic version: System.Web.Mvc.ViewPage<Employee>
. Make sure you compile your project after updating the first few lines of code in...