Trying to use two pages of layout on MVC4 and Razor

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

I do not understand this since I am new to MVC.

What should I do?

Thanks!

+4
source share
3 answers

If you use the Renderbody file in the Renderbody file, make it like a layout page.

And add another partial page called MyPartial.cshtml named Layout as _Sistema.cshtml.

Renderbody should only be on the main page. those. layout page.

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

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

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

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

Then use your partial page in your view as follows:

 @Html.Partial("MyPartial") 

Hope this helps.

+3
source

RenderBody is for masters only. This method displays the layout of content pages that are not related to a specific section. If your opinion calls RenderBody , there are two possible cases:

  • Either this is a mistake, and this opinion should not be called.
  • Or this view is a master, and instead you should use some other views that inherit the layout from this master.
+3
source

you just need to interact with _ViewStart.cshtml

and use if the condition is to specify a sharing page for each user group. for example, admin user, then user _Layout.cshtm is another wise to use _Layout2.cshtml

use the following code:

 @{ if(User.Identity.Name.ToLower()=="admin") { Layout = "~/Views/Shared/_Layout2.cshtml"; } else { Layout = "~/Views/Shared/_Layout.cshtml"; } } 
+2
source

Source: https://habr.com/ru/post/1488715/


All Articles