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;

Comments in Razor -

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

For each loop in Razor -

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

If Else block in Razor -

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

                             //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 -

Share on Google Plus

Please write your comment below. Share this blog on Social media.


Post a Comment

Dear Readers, Please post your valuable feedback in the comment section if you like this blog or if you have any suggestions. I would love to hear the same from you. Thanks