Display navigation in MVC3

What is the best way to visualize navigation in MVC3? In my application, the controller should decide what should be in the navigation, but as far as I know, there is no way to transfer the model to the _Layout file (where html Navigation lives) to provide it with this information.

+3
source share
3 answers

You can make the model available to your layout.

  • Define the base class MyBaseModel with the properties you want to use in the layout.
  • Fake your subclass of MyBaseModel and make sure you populate the properties
  • Specify your layout @model MyBaseModel
  • Use properties

. , ( " , " ),

+6

. , druttka, - RenderAction()

<div>@Html.RenderAction("action", "controller")</div>
+4

@druttka. , .

, 2 :

  • , .

, , , . . , , . , OnActionExecuted nav, .

An alternative is to add nav to ViewBag. This can be done every time and does not force you to specify a model for each view, which is great for flexibility in cases where you do not need to specify a model. It should be noted, however, that ViewBagin asp.net mvc 3 is of type dynamic, which cannot be used as a parameter in a lambda function, thereby preventing you from doing something like @Html.DisplayFor(viewBag => viewBag.MainNav)in your layout, which is real resistance. You can still partialize and specify the appropriate DisplayTemplate.

+3
source

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


All Articles