Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP MVC 3 use different Layouts in different views

I have an ASP MVC application which needs multiple different layouts. In ASP.NET Web Apps I would have just made separate master pages. How do I do this in ASP MVC 3?

So far I have made a separate Layout.cshtml file for each layout I need.

I tried setting the layout in the view but it is getting blown away from the ViewStart.cshtml which is setting it back to the default layout for the site.

Also, I can't seem to get intellisense working with Razor so I haven't been able to explore much of what I can do in the ViewStart, if I can conditionally set the Layout, or what.

Thoughts?

like image 998
Adam Avatar asked Feb 20 '11 19:02

Adam


People also ask

Can we use multiple layout in MVC?

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. In some cases, we can have the requirement to use multiple shared layout pages in MVC application.

How do you specify a common layout for all views?

The default _ViewStart. cshtml is included in the Views folder. It can also be created in all other Views sub-folders. It is used to specify common settings for all the views under a folder and sub-folders where it is created.


1 Answers

You could set the layout dynamically in your controller action:

public ActionResult Index() {     var viewModel = ...     return View("Index", "_SomeSpecialLayout", viewModel); } 
like image 149
Darin Dimitrov Avatar answered Nov 03 '22 23:11

Darin Dimitrov