Home/MVC/Different ways of rendering layout in MVC

Different ways of rendering layout in MVC

What is Layouts?

Layouts is same like as form which may used commonly among all the pages. Layout are like Master page in web form.

What is the use?

Using Layouts, we can maintain consistent look and feel across the pages/views within our site/application. It contains common html,css,content…

MVC Layouts - dotnet-helpers

Rendering layout in different ways :

Let we discuss about the different ways of rendering layout in our forms.

Render Layout 1 : Mention Layout with in each view on the top

The default layout has been added while creating the view. We can override the default layout by assigning our own layout like as shown below

Layout = “~/Views/Shared/_CustomerLayout.cshtml”;

Render Layout 2 : Returning the Layout in controller –> ActionResult

Here we can override default layout by rendering the layout from the ActionResult. From below code, the layout _customerLayout has been replaced in the Index view.

public ActionResult Index()
return View(“Index”, “_CustomerLayout”, model);

Render Layout 3 : Define _ViewStart in each Folder/directory

We can set default layout for particular directory/folder by creating _ViewStart file in each of the directories/folder with the piece of code as shown below

//Name of the Layout which want to be set as default
Layout = “~/Views/Shared/_Layout.cshtml”;


MVC Custome Layouts - dotnet-helpers


READ  How to create simple WebGrid in MVC (Razor engine)

After creating _viewStart.cshtml in each directory, it will be set as default layout while creating new view in the same directory.

Keep cool coding….


Leave A Comment