Dynamic content based on @RenderBody?

I have a main page that uses @RenderBody to display the current contents of the \ action controller. I am faced with a situation where I want to show a partial view, depending on which controller is displayed using @RenderBody. Is this possible with @RenderAction or @RenderPartial? Thanks

enter image description here

+4
source share
2 answers

You can take a look at the function Sections (RenderSection). Well described by Scott Gu http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

+3
source

Go to your general views and inside @Html.Partial("_displayCustomPartial") put @Html.Partial("_displayCustomPartial") . Then go back to the general views folder and create a new _displayCustomPartial view. Open _displayCustomPartial.cshtml and then use this code inside it:

 @{ var controllerCalled = ViewContext.Controller.ValueProvider.GetValue("controller").RawValue; var actionCalled = ViewContext.Controller.ValueProvider.GetValue("action").RawValue; switch(controllerCalled){ case "Home": @Html.Partial("_homePartial"); break; case "Work": @Html.Partial("_workPartial"); break; case default:break; } } 

This scenario assumes that ready-made views are ready for each controller script (I also included an action code if you want to use it). If the finished views are not ready, just enter the code that should be displayed for each case, and not to display a different view.

The main difference between this and the sections is that the sections share the model with their representation, and the use of partial representations would allow the inclusion of a separate model.

+1
source

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


All Articles