I have a layout page, which when the RenderBody is called, searches for my Home/Index.shtml view. I now however, want my RenderBody to look for Home/Default.shtml instead, when the site starts up.
How can I change the view associated with the default RenderBody of my layout page?
You can dynamically choose the layout template from either:
1) In the action method passing the name of the layout file to View() -- it's one of the params in one of the overloaded versions.
2) Dynamically change the Layout in _ViewStart.cshtml. The _ViewStart has access to ViewContext which in turn has access to ViewData -- you could set a flag in there and use it from _ViewStart.
The layout page doesn't actually have anything to do with which view is being loaded. The view determines which layoutpage it belongs to and identifies the body of the view (which gets called in RenderBody).
To get a default view you need to either create a new Action method and set that as the default action in the route table, or simply specifiy a different view in the Index Action. For example:
public ActionResult Index(){
return View("Default");
}
This will return the file Default.cshtml instead of Index.cshtml.
Again, if you don't want to specify a view , then create a Default Action and set it as the default action in the route table:
public ActionResult Default(){
return View();
}
//Default route
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Default", id = UrlParameter.Optional } // Parameter defaults
);
Goalie35
Member
194 Points
192 Posts
MVC3: Changing the start page/view?
Mar 27, 2012 04:40 PM|LINK
I have a layout page, which when the RenderBody is called, searches for my Home/Index.shtml view. I now however, want my RenderBody to look for Home/Default.shtml instead, when the site starts up.
How can I change the view associated with the default RenderBody of my layout page?
Thanks.
BrockAllen
All-Star
27574 Points
4912 Posts
MVP
Re: MVC3: Changing the start page/view?
Mar 27, 2012 04:45 PM|LINK
You can dynamically choose the layout template from either:
1) In the action method passing the name of the layout file to View() -- it's one of the params in one of the overloaded versions.
2) Dynamically change the Layout in _ViewStart.cshtml. The _ViewStart has access to ViewContext which in turn has access to ViewData -- you could set a flag in there and use it from _ViewStart.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
CodeHobo
All-Star
18647 Points
2647 Posts
Re: MVC3: Changing the start page/view?
Mar 27, 2012 05:06 PM|LINK
The layout page doesn't actually have anything to do with which view is being loaded. The view determines which layoutpage it belongs to and identifies the body of the view (which gets called in RenderBody).
To get a default view you need to either create a new Action method and set that as the default action in the route table, or simply specifiy a different view in the Index Action. For example:
public ActionResult Index(){ return View("Default"); }This will return the file Default.cshtml instead of Index.cshtml.
Again, if you don't want to specify a view , then create a Default Action and set it as the default action in the route table:
public ActionResult Default(){ return View(); } //Default route routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Default", id = UrlParameter.Optional } // Parameter defaults );Blog | Twitter : @Hattan