Exercise 2: Using Models in Razor Views
In this exercise, you will work with models in Razor views. Initially, you will use the @inherits directive that was released as part of MVC 3.0, and then the new @model directive that is now supported in the Razor engine. These directives provide a simplified way to reference your strongly-typed models in your view files. You will add view templates used by action methods to browse the albums in the music store.
Task 1 – Using the @inherits Directive
In this task, you will replace the Index action method of the HomeController to retrieve the Date & time formatted in the webserver’s culture.
Task 2 – Running the Application
Task 3 – Adding a View Template in Razor
In this task, you will add a new Index.cshtml|vbhtml view, in this case, using the @model directive. This directive, represents an improvement for the @inherits, simplifying the use of strongly-typed model classes in the view. You simply write @model StrongModelTypeat the top of the cshtml file, replacing the @inherits directive. You will add a view template in Razor for the Store Index page, which shows a list of Genres. At the end of the task you should get the same functionality you had when using ASPX templates, but with a simplified code.
Task 4 – Running the Application
Task 5 – Creating Browse CSHTML View and Adding Details CSHTML View
Task 6 – Running the Application
In this task, you will run the application to verify that Razor views are displayed as expected.