Razor View and ASPx in MVC

In MVC 5 we have only one way to create View which is Razor View, the file extension of razor view is ".cshtml"
But before MVC 5 we had one more option which was ASPX, now in MVC 5 ASPX has been removed and currently, Razor view is available with MVC 5.

Razor View vs ASPx in MVC

Razor View was introduced in MVC 3.
There were differences in ASPX and Razor view.
We use @{} to write dynamic code in Razor view and <% %> to write dynamic code in ASPx.

Expression block in Razor View engine -


@{
    int x = 10;
    int y = 5;
    int z = x + y;
    @z;
}

Comments in Razor -

@*  @Html.Raw(@Model.EmployeeName)*@

For each loop in Razor -

@foreach (var item in Model)
{
    <div class="itemdata">
        <div>
            <div>
                    <img src="@item.ImagePath" />
            </div>
            <div class="cel_post_txt">
                @item.Title<br>
            </div>
        </div>
    </div>
}

If Else block in Razor -


 @if (Convert.ToString(HttpContext.Current.Session["UserRole"]) == "")
                        {
                             //Hide or Show some menu
                        }

 else
                        {
                             //Hide or Show some menu
                        }


How to pass data from Controller to View:

  • ViewBag
  • ViewData
  • TempData


I will discuss these 3 ways one by one in next blogs.

Next -


Comments

Popular