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

 

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….