0

I have a partial view named '_MenuPartial' which has dynamic data from the database.Now I render this view in the '_Layout.cshtml' as below

_Layout.cshtml:

@Html.Partial("_MenuPartial");

in my controller, I will send the menu data model to the 'index.view' which is based on the '_Layout.cshtml'.

But My question is that when I want to set the Layout for other views, I need send the menu data model in other views' controller which I think is redundant. So What's the standard way to sent the data model to partial view in the Layout page? How to response a view which has the layout but don't give the data model layout page need?

Hope your answer. thanks a lot!

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
Jason.Chen
  • 19
  • 4
  • Use `@Html.Action(...)` to call a `ChildActionOnly` server method that returns the partial view. –  May 02 '17 at 08:53
  • @StephenMuecke, Could you give much more details about your answer? – Jason.Chen May 02 '17 at 09:02
  • Refer the answers [here](http://stackoverflow.com/questions/33542818/dynamic-menu-from-database/33557835#33557835) and [here](http://stackoverflow.com/questions/32063808/applying-security-among-menus/32065212#32065212) for some examples –  May 02 '17 at 09:05
  • @StephenMuecke, Thanks a lot! that's exactly what I want – Jason.Chen May 20 '17 at 07:14

1 Answers1

0

you can pass your model like below,

@Html.Partial("_MenuPartial", model)
Kinjal Gohil
  • 958
  • 9
  • 15
  • I know I can pass the model like this for partial view, but I don't want to do this every time I call the view rendered the layout because this partial view is in the Layout.cshtml – Jason.Chen May 02 '17 at 09:30
  • so you can define this model top of your partial view like @model Model and pass your model from controller return view(model) like this – Kinjal Gohil May 03 '17 at 10:03