Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 383
GustavoAdolfo Avatar asked Feb 16 '23 02:02

GustavoAdolfo


2 Answers

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.

like image 124
Naren Avatar answered Feb 18 '23 16:02

Naren


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.
like image 32
Andrei Avatar answered Feb 18 '23 14:02

Andrei