The Razor View Engine allows us to use Razor templates to create views that are a combination of HTML and C# code. Razor templates use the .cshtml file extension. HTML is written normally, but to integrate C# code you need to use code expressions or code blocks, which will be explained later in this blog post.
A quick way to add a new view for an action inside a controller is to right click inside that action method and choose Add View. A window will pop up where you can customize the view you're creating. Remember to pick Razor as the view engine type.
You have the option to "Create a strongly-typed view", which allows you to choose the type of model object you'll be passing into the view. By specifying the type, the created view will add the appropriate directive for the model for you. Also, if you check this option, you can select a scaffold template to create a basic UI for displaying properties of the model object.
Note: Razor will automatically HTML encode data sent to the view to help prevent cross site scripting (xss) attacks.
Code Expressions
Code expressions can be used in Razor views to pull out data from your model or contain expressions for Razor to evaluate and then display as text. The "@" symbol is used to indicate a code expression.
If your model is enumerable, a couple methods are available to you.
<!-- The model is a list of restaurant reviews --> @model IEnumerable <!-- .First() gives you the first review in the list --> <p>Name of the first reviewer: @Model.First().Name</p> <!-- .Last() give you the last review in the list --> <p>Rating from the last review: @Model.Last().Rating</p> <!-- .Count() returns the number of reviews in the list --> <p>Number of reviews for the restaurant: @Model.Count()</p>
Note: You can add comments to .cshtml files, which will not be visible in the output html file by placing the comment between "@*" and "*@".
@* Commented out code *@
Implicit vs Explicit C# Code Expressions
When using implicit C# code, we let Razor figure out what is C# code and what is markup. Razor is able to parse the line of code below correctly.
<p>The number is @item.Number</p>
But what if you wanted to divide the number by 10? Doing the code below will display "/ 10" as text and not include it in the C# expression for evaluation.
<span>@item.Number / 10</span>
This is where explicit C# code expressions are useful. Simply wrap the entire expression in parentheses after the "@" symbol as seen below. Now the @item.Number is divided by 10 and then displayed.
<span>The number divided by 10 is @(item.Number / 10)</span>
Razor is generally good at differentiating between C# code and markup, but there are a couple gotchas. Below are some of the common ones.
If you want to add the "@" symbol as text before some text, such as a twitter handle, you will need to escape the "@" sign via another "@" symbol. This way, it doesn't think the twitter handle is C# code that needs to be evaluated.
<p>Twitter Handle: @@TwitterName</p>
Another gotcha is when you use want to append some text, such as the letter 'R' to a rating score stored in a model property. You want it to display something like "R5". The below code will NOT work.
<span>Incorrect: [email protected]</span>
It will assume "@item.Rating" is text. This assumption is default so it is easier to display email addresses which follow the format of something@something. To get around this, just use explicit C# expressions.
<span>Correct: R@(Item.Rating)</span>
Tidbit: The @VirtualPath property is inherited by views and can be used to display the virtual path of the view.
Code Blocks
You can make C# code blocks in .cshtml files to write multiple lines of consecutive C# code. Code blocks allow you to do the same things you can do in .cs files. You can change properties of the ViewBag and even declare variables, which can be used anywhere in the .cshtml file after the variable declaration.
@ { ViewBag.Title = "New Title"; int num = 5; }
For each statements work a little differently to make it easier to read, write, and transition between C# code and markup. We can use the for each statement to iterate through a list of items in a model and display their properties one by one. Razor is smart enough to differentiate between C# and the table markup so we can write code like the example below.
To solve this problem, you can indicate that "Review" is literal text by adding "@:" before it as shown below.
@:Review
Reminder: Try to keep the view simple. Usually having a lot of C# code in the View is a bad sign. Try to keep most of the heavy lifting and logic inside the Models and Controllers.
Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
✓ Live Streaming✓ Interactive Chat✓ Private Shows✓ HD Quality
Anya is LIVE right now
FREE
Free to watch • No registration required • HD streaming