w3resource

How to use Layout View in ASP.NET MVC


You may have wondered and asking: how would the View know which layout view to use? Can I have different layouts in my application? All these will be answered shortly.

There are three ways of telling your application which layout view to use in ASP.NET application. You can either set up the layout view by using _ViewStart.cshtml file or by setting up the path of the layout page using Layout property in the individual view or specifying layout view name in the action method.

_ViewStart.cshtml

By default, _ViewStart.cshtml is sets up the default layout page for all the views in the folder and its subfolders in your ASP.NET MVC application. You can assign a valid path of any Layout page of choice using the Layout property.

asp.net _ViewStart.cshtml

From the screenshot above, the _ViewStart.cshtml in the Views folder, sets the Layout property to "~/Views/Shared/_Layout.cshtml".

Thus by default, _layout.cshtml would be layout view of all the views included in Views and its subfolders unless other wise stated in the view.

Setting Layout property in individual view

We can override the default layout page set by _ViewStart.cshtml by setting Layout property in each individual .cshtml view. For example, the following Index view use _IndexLayoutPage.cshtml even though _ViewStart.cshtml sets _Layout.cshtml as the default layout page.

@{
    ViewBag.Title = "Index Page";
    Layout = "~/Views/Shared/_IndexLayoutPage.cshtml";
}
<div class="jumbotron">
    <h1>ASP.NET</h1>
    <p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
    <p><a href="http://asp.net" class="btn btn-primary btn-lg">Learn more »</a></p>
</div>
<div class="row">
    <div class="col-md-4">
        <h2>Getting started</h2>
        <p>
            ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that
            enables a clean separation of concerns and gives you full control over markup
            for enjoyable, agile development.
        </p>
        <p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301865">Learn more »</a></p>
    </div>
</div>

Specifying Layout Page in ActionResult Method

Aside specifying the layout page in the _ViewStart.cshtml or in the Individual views, we can also specify which layout page to use in while rendering view directly from the action method using View() method.

The following example, we will use the View() method to render the Index view using _IndexLayoutPage.cshtml as the layout page.

//Example: Specify Layout View in Action Method
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View("Index", "_IndexLayoutPage");
    }
}

Rendering Methods

To render child views, ASP.NET MVC renders child views using the following methods.

Method Description
RenderBody() Renders the portion of the child view that is not within a named section. Layout view must include RenderBody() method.
RenderSection(string name) Renders a content of named section and specifies whether the section is required. RenderSection() is optional in Layout view by default. To display this section in the desired location, we use the @section SectionName{} command, and all the contents of the section goes into the braces.


Inviting useful, relevant, well-written and unique guest posts