Layout in ASP.Net MVC

Sidenote : This is the part 4 of the ASP.Net MVC course series.  You can get all the parts of this tutorial here.

While adding view for Index action method, we have unchecked the ‘Use a layout page’ as in below screenshot.

Add View

 

We have unchecked this option, as we don’t know what a Layout page is and how it will help us in making web application development better/easier.

Let me explain.  Add another action method ‘MovieOfTheMonth’ which shows highest rated movie as of this month.

I have added the below code to the MovieController


public ActionResult MovieOfTheMonth()

{

            return View();

 }

Right click inside the above action method and select ‘Add View’ option.

Make sure ‘Use a layout page’ option is unchecked as we did to Index Action method and click Add. Below code would have generated for you.

MovieOfTheMonth cshtml

 

I have added the below code in the view(within the above div) to show the Movie of the month to the user.


  @{

            var movieOfTheMonth = new { Rank = 1, Title = "The Shawshank Redemption", Year = 1994 };

        }

        <b>Movie of the month: @movieOfTheMonth.Title </b>

Under normal circumstances, we will be getting this information by querying the database and get the highest rated movie as of this month. As our objective is to create view with dummy data, I have just created an anonymous object and use that object to display movie of the month to the user.

In our MicroIMDB application, we have 2 action methods in our MovieController

  1. Index action method – to display the list of movies
  2. MovieOfTheMonth action method – to display top rated movie as of this month

Now, we want to show some banner image at top of each page. We are not doing anything original here. Most of the websites, including original IMDB, has the below section (containing search box and other links) on every page – whether you are searching for movie or looking for information on any person.

 

IMDB Banner

 

To do so in our MicroIMDB application, we need to make changes in both pages (on both Movie list page Movie of the month page). Think about if we have dozens of pages – yes, we need to make the changes in dozens of pages.

Layout solves this problem by defining the structure of the page. Layout brings the consistent structure to all the pages of the application and results in better user experience.

In the below picture, top and side sections are relatively static whereas the content section would be changed for every page.

Layout

Let us see an example how layout is being used in MSDN website (ASP.Net MVC4 reference)

We have top section where they have made links to top level categories(Technologies, Programs etc..) and we have links of sub-categories related to top level category chosen(I chose Documentation). When you click some link in left hand side section (say System.Web.Mvc.Ajax) , MVC ajax information is shown in the content page – everything else in the page remains the same. If you have defined layout page, you can concentrate on only building the content page – rather than building the whole page.

ASP.Net MVC msdn Layout

 

 

In both of our Views (Index and MovieOfTheMonth), we have assigned Layout = null at the top of the view like below – saying the view engine not to use any layout for the view.


@{

Layout = null;

}

All the information for that page would be rendered from that view itself. As discussed earlier, if you want to change something (like making menu at top of every page), you need to make changes in all the pages. If you use Layout, we can make changes in the layout and the change would be reflected in all the pages if you use that layout.

Layout - View

 

 

When you select Empty MVC template, it creates a Layout for us be default. But we decided not to use the layout option by unchecking ‘Use a layout page’ option when adding view.

How can we use the layout in our Index and MovieOfTheMonth views now?

We just have to make two changes.

  1. Remove the line which sets Layout to null or comment it.
  2. Remove all generic code – not specific to the view

After making the changes, Index.cshtml would like below.


@{

//Layout = null;

&nbsp;

//Create anonymous movie type

var movie1 = new { Rank = 1, Title = "The Shawshank Redemption", Year = 1994 };

var movie2 = new { Rank = 2, Title = "The Godfather", Year = 1972 };

var movie3 = new { Rank = 3, Title = "The Godfather - Part II", Year = 1974 };

var movie4 = new { Rank = 4, Title = "The Dark Knight", Year = 2008 };

var movie5 = new { Rank = 5, Title = "Pulp Fiction", Year = 1994 };

&nbsp;

//Create a list out of those 5 movies.

var moviesList = new[] { movie1, movie2, movie3, movie4, movie5 }.ToList();

&nbsp;

}

&nbsp;

<b> Movies List:</b><br/>

<table border="1">

<tr>

<th>Rank</th>

<th>Title</th>

<th>Year</th>

</tr>

@foreach(var movie in moviesList)

{

<tr>

<td>@movie.Rank</td>

<td>@movie.Title</td>

<td>@movie.Year</td>

</tr>

}

</table>

Index - With Layout

 

The top menu bar with text “Application name” and copyright symbol at the bottom of the page is part of layout and thus will be available at every page. Our content(movie list) is displayed with the layout content as in the above screenshot.

But wait… We’ve not defined any layout.

How ASP.Net MVC does gets this layout content?

It’s because of convention over configuration of ASP.Net MVC. By convention, ASP.Net MVC using the layout page as defined in Views\_ViewStart.cshtml. See the note  in the below screenshot

Add View

Below is the content of _ViewStart.cshtml


@{

Layout = "~/Views/Shared/_Layout.cshtml";

}

It says, use the Layout defined in _Layout.cshtml for all the pages. When we remove Layout=null from our views, it uses this layout by convention – we don’t have to do anything.

Open the file Views/Shared/_Layout.cshtml. It contains the layout information for our application.

There are many things which you may not understand in this layout file. Don’t worry.

The only thing you need to understand is that when view engine sees @RenderBody in template, it would replace the content view(index.cshtml) and send the merged view to the controller.

There is no one layout per application restriction. You can create/use multiple layouts in our application based on your needs.

Previous Chapter Home Next Chapter [x_subscribe form=”398″]