c# - asp.net mvc - how can I have multiple viewmodels bound to a view -
this question has answer here:
- multiple models in view 9 answers
i new c# please excuse dumb question. creating webapp using asp.net mvc. on nav bar have button brings modal containing form. done in _layouts.cshtml shared view. capture form post use viewsmodel passes controller processing. problem loads model every view loaded in webapp unless use separate shared view. stops me passing model controller view.
at moment have created model named masterviewmodel has other view models properties, still stops me passing models view...
does have advise on how around problem?
yes! can!
there many ways there 2 fancy most.
1.) viewbag
with 1 simple, can set view bag in controller along lines of viewbag.example = yourviewmodelhere
then access on view saying @viewbag.example.attribute
2.) partial view
another way keep architecture organized nicely , have 1 view model per view. (i prefer method)
this way can stick way have been handing single view model single page. reason tend use way most, 1 not better other. more of preference in end.
3.) wrapper class
wrap view models in parent class , pass parrent class in view model. allow access individual view models such saying:
@model.viewmodela @model.viewmodelb
for individual examples of different ways check out post. helped me when in shoes: https://stackoverflow.com/a/4765113/5874935
Comments
Post a Comment