Today, in this discussion, we’ll start with Razor views. The Razor view engine allows user to produce HTML output. Basically it’s a combination of data and html template. These are the basically files which endup with .cshtml extension. So, in order to put the data on the markup, I’ll use C# sharp code in the razor view. Now, in order to better explain the same let’s go ahead a movie reviews controller and select the option with empty read/write as shown below.
Now, let me go ahead and one model class which will be used with my controller and i call this class as MovieReview and i’ll put certain property in there to expose the same as shown below.
But, right now, in order to just test the same, we need some sort of in memory data. we’ll add the same in the controller itself as shown below.
now, what i need to do is use the same code above in my index action to list the movie based on the name. so, for doing the same i’ll use a little linq query to achieve the same as shown below in the example
Now, since i have not added view for the same so far, so i’ll add a view for the same so that it can be rendered in the browser. now, for doing the same i’ll rt-click on the index action and say Add view, but this time i want to create an strongly typed view so that my model class gets available in my view like shown below and i’ll also select list from the scaffolding template so that it will generate code required for the same in the view as shown below.
so, this is a basic view which is strongly typed against the Movie Review. and now, this is simply going to create a table with all the properties in there. In the next section, we’ll see that how to optimize the view and play with the razor syntax. Till then stay tuned and happy coding.
1,115 total views, 2 views today