Search code examples
asp.net-mvc-3asp.net-mvc-3-areas

want to put @renderbody() into a partial view source by _layout


Creating an MVC 3 Razor project. have a very involved UI design. I wanted to put @renderbody() into a partial view source by _layout. The compiler won't let me. Is there a way to do this?


Solution

  • Instead of partial view you can go for master/sub layouts.

    MasterLayout.cshtml

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8" />
      <title>@ViewBag.Title</title>
    </head>
    <body>
      @RenderBody()
    </body>
    </html>
    

    Layout.cshtml

    @{
      Layout = "~/Views/Shared/_MasterLayout.cshtml";
    }
    
    // html code above render body
    @RenderBody()
    // html code below render body
    

    Your sublayout(Layout.cshtml) contains the code that should be in the partial view.