Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change @RenderBody to point to different View MVC3

By default, the @RenderBody in _Layout.cshtml in an MVC3 app points to ~/Views/Home/Index.

  @RenderBody()

Where is this set and how do I change it to point to ~/Views/Account/Logon? Or wherever I want. Thanks

like image 963
BoundForGlory Avatar asked May 23 '12 20:05

BoundForGlory


People also ask

How do I render a view in another view in MVC?

To create a partial view, right click on the Shared folder -> click Add -> click View.. to open the Add View popup, as shown below. You can create a partial view in any View folder. However, it is recommended to create all your partial views in the Shared folder so that they can be used in multiple views.

What does RenderBody () mean?

RenderBody() is called to render the content of a child view. Any content on said view that is not in a @section declaration will be rendered by RenderBody() . Using the Layout view above, that means that all content in a child view will be rendered inside the <div class="container body-content"> .


2 Answers

It doesn't point to that view, it merely renders the view that it is given

Your app starts up and goes to the default action on the routing which can be found in Global.asax

You can modify that to default to /Account/LogOn if you wish

public class MvcApplication : System.Web.HttpApplication {

    public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
        filters.Add(new HandleErrorAttribute());
    }

    public static void RegisterRoutes(RouteCollection routes) {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        //routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Account", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
        );
    }

    protected void Application_Start() {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }
}
like image 95
CD Smith Avatar answered Oct 23 '22 14:10

CD Smith


RenderBody doesn't point by default to ~/Views/Home/Index. It renders the view that was returned by the controller action that was executed. And since in your Global.asax in the routing definition the default action is configured to be Index, it is this view that is rendered.

So all you have to do is modify your routing configuration so that the default action is Logon on the Account controller:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Account", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
);

Now when you navigate to /, the LogOn action of the Account controller will be executed which itself will render the ~/Views/Account/LogOn.cshtml view.

like image 35
Darin Dimitrov Avatar answered Oct 23 '22 13:10

Darin Dimitrov