Is there a way to set the layout from the controller?
have tried:
ViewData["Layout"] = "..."
return View("view", Model);
I know it will sound odd with some people....
Is there a way to set the layout from the controller?
have tried:
ViewData["Layout"] = "..."
return View("view", Model);
I know it will sound odd with some people....
View method has overload to set its master layout something like this
return View ("NameOfView",masterName:"viewName");
In action method you can use MasterName property in ViewResult class to change layout page.
public ActionResult Index()
{
var myView = View();
myView.MasterName = "~/Views/Shared/_Layout2.cshtml";
return myView;
}
Using your code, you could put this in your View:
@ {
Layout = ViewData["Layout"];
}
Daren Dimitrov has a very nice answer on this one with Attributes:
How do I specify different Layouts in the ASP.NET MVC 3 razor ViewStart file?
In the controller you can set a masterpage like this. I'm using MVC 5.2
return View("ViewName", "MasterPageName", model)
If you have a _ViewStart.cshtml file in your Views directory, you can automatically set the layout for all views within the same folder (and sub-folders):
@{
Layout = "~/Views/Shared/Layout.cshtml";
}