Wednesday, August 31, 2016

MVC multiple layouts

Issue:

Working an MVC project which required us to use different layouts.  I knew you could do it but was not sure of the syntax. 

Solution:

It turns out there are at least 4 ways to do this.  I only am mentioning the solution we used since it is what we needed.  Feel free to the Net Stuff blog for more details.

Set Layout by Returning from ActionResult

You can override the default layout rendering by returning the layout from the ActionResult. So, this is also a way to render different Layout in your ASP.NET MVC application.




public ActionResult Index()
{
 SampleModel model = new SampleModel();
 //Any Logic
 return View("Index", "_WebmasterLayout", model);
}


Source:

http://www.dotnet-stuff.com/tutorials/aspnet-mvc/how-to-render-different-layout-in-asp-net-mvc