razor syntax ASP.NET MVC

www.igif‮aedit‬.com

Razor is a view engine that is used in ASP.NET MVC to create dynamic HTML pages. Razor uses a combination of HTML markup and C# or VB.NET code to generate the final output that is sent to the client's browser.

Here are some examples of Razor syntax:

  1. Outputting plain text or HTML markup:
<p>Hello, World!</p>
  1. Outputting a variable's value:
<p>My name is @Model.Name</p>
  1. Using a conditional statement:
@if (Model.Age >= 18) {
    <p>You are an adult</p>
} else {
    <p>You are a minor</p>
}
  1. Looping through a collection:
@foreach (var item in Model.Items) {
    - @item.Name

}
  1. Using a helper method:
@Html.ActionLink("Click me!", "Action", "Controller")
  1. Defining a section of the layout to be replaced by the view:
@section Scripts {
    <script src="myscript.js"></script>
}

These are just a few examples of the syntax used in Razor. The Razor syntax is designed to be easy to read and write, and it allows for a lot of flexibility in creating dynamic web pages in ASP.NET MVC.