Asp.net-mvc – ASP MVC 3 use different Layouts in different views

asp.net-mvcrazor

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?

Best Answer

You could set the layout dynamically in your controller action:

public ActionResult Index()
{
    var viewModel = ...
    return View("Index", "_SomeSpecialLayout", viewModel);
}