4

When I use @RenderBody on a view (not principal), I receive this message Error: The file "~/Views/Shared/_Sistema.cshtml" cannot be requested directly because it calls the "RenderBody" method.

I do not understand it as I am a novice in MVC.

What do I can do?

Thanks!

tereško
  • 58,060
  • 25
  • 98
  • 150
GustavoAdolfo
  • 361
  • 1
  • 9
  • 23
  • i was searching for the same today in case you didn't get the perfect answer yet try this http://stackoverflow.com/questions/5161380/how-do-i-specify-different-layouts-in-the-asp-net-mvc-3-razor-viewstart-file – Ali Jan 08 '14 at 11:07

4 Answers4

3

RenderBody is for masters only. This method renders markup of content pages that does not belong to any particular section. If your view calls RenderBody, two cases are possible:

  1. Either this is a mistake and this view should not call it.
  2. Or this view is a master, and you should instead use some other views inheriting layout from this master.
Andrei
  • 55,890
  • 9
  • 87
  • 108
  • Thank you very much @Andrei. well... I need implement two differents views. Have a form do make it? – GustavoAdolfo Jun 28 '13 at 16:03
  • @GustavoAdolfo, just add two new views to the project, and set their Layout to be the master you want, most likely `_Sistema.cshtml`. I believe that every tutorial on ASP.NET MVC for masters covers that scenario. – Andrei Jun 28 '13 at 16:08
3

If you are using the Renderbody in _Sistema.cshtml file, then make it as a Layout page.

And add another partial page named like MyPartial.cshtml with Layout name as _Sistema.cshtml.

Renderbody is supposed to be in the master page only. i.e., Layout page.

So your _Sistema.cshtml page should only contains the following:

@RenderBody()
@RenderSection("scripts", required: false) @*----Optional---*@

Then your your new partial page MyPartial.cshtml should contain the following:

@{
    Layout = "~/_Sistema.cshtml";
}

Then use your partial page in your view as follows:

@Html.Partial("MyPartial")

Hope it helps.

Naren
  • 1,300
  • 14
  • 32
  • If my comments took you home, kindly mark it as answer, so that others can also prefer the same. Thank you. – Naren Mar 15 '14 at 05:12
2

you just need to interact with _ViewStart.cshtml

and use if condition to specify the share mater page for each group of users. for example user is admin then user _Layout.cshtm other wise use _Layout2.cshtml

use following code :

@{
    if(User.Identity.Name.ToLower()=="admin") {
        Layout = "~/Views/Shared/_Layout2.cshtml";
    }
    else {
        Layout = "~/Views/Shared/_Layout.cshtml";
    } 
}
Matthew
  • 9,851
  • 4
  • 46
  • 77
0

In my experience, I was struggling with this same issue for days. After further investigation, I found that when starting a new Umbraco site, I had the option to select templates. That resolved the RenderBody issue. When creating the layout page without the templates it doesn't see the Master page as the layout, hence not being able to call the RenderBody method. Using the templates automatically sets the Master page as the Layout allowing you to call the RenderBody. I hope this helps.

Revan
  • 1