4

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?

Dave Alperovich
  • 32,320
  • 8
  • 79
  • 101
  • No. You can use `@RenderBody` only in layouts. – VJAI Aug 24 '12 at 13:57
  • thats what i was afraid of. thanks. – Dave Alperovich Aug 24 '12 at 14:00
  • Can you add little more detail of what you are trying to achieve through partial views? – VJAI Aug 24 '12 at 14:01
  • ideally i want to move a big block of html (a table within a div) in a partial view. I want this markup to wrap the @renderbody(). If I can't put the @renderbody() in a partial, then the markup must be left in _layout or split between 2 partials which is messy and confusing. – Dave Alperovich Aug 24 '12 at 14:05

1 Answers1

8

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.

VJAI
  • 32,167
  • 23
  • 102
  • 164