Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Layout(Master Page) of view in ASP.NET MVC without recreate it

I'm using ASP.NET MVC 3 with Razor views. When you want to create a view you can choose a layout (master page) for your view, or leave it to choose Default (_Layout).

I am interesting in to change this layout after create a view without recreate it, is there any where to store the layout information about the views? and how can I change it?

like image 746
Saeid Avatar asked Dec 15 '11 06:12

Saeid


People also ask

How do I apply master layout to a view?

Creating a View Master Page You add a new view master page to an MVC project by right-clicking the Views\Shared folder, selecting the menu option Add, New Item, and selecting the MVC View Master Page template (see Figure 1). You can create more than one view master page in an application.

Can we have multiple layout pages in MVC?

Q. Can we use multiple Layout pages in a single MVC application? Yes, we can use multiple Layout in ASP.Net MVC application. By default, Visual Studio adds a Layout page in a shared folder which can be used by other View pages if required.


1 Answers

In MVC3 you have _ViewStart.cshtml that stores all pages' Layout; you can change this element to change all pages' Layout or you can add new Layout element in top of target view pages in @{} block like the following to change the layout of the specific page:

@{     Layout = "~/Views/Shared/_newLayout.cshtml";     ViewBag.Title = "Index"; } 
like image 163
Saeid Avatar answered Sep 19 '22 01:09

Saeid