GustavoAdolfo
GustavoAdolfo

Reputation: 378

Trying to use two layout pages on MVC4 and Razor

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!

Upvotes: 4

Views: 11837

Answers (4)

Revan
Revan

Reputation: 1

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.

Upvotes: 0

Naren
Naren

Reputation: 1350

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.

Upvotes: 3

Rashid Mehmood
Rashid Mehmood

Reputation: 21

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";
    } 
}

Upvotes: 2

Andrei
Andrei

Reputation: 56688

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.

Upvotes: 3

Related Questions