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
  • 57,247
  • 24
  • 95
  • 149
GustavoAdolfo
  • 337
  • 1
  • 8
  • 20
  • 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

3 Answers3

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
  • 54,517
  • 9
  • 84
  • 106
  • 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,250
  • 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,651
  • 4
  • 43
  • 75