Last post Dec 08, 2016 12:25 PM by FJCrux
Dec 08, 2016 12:08 PM|FJCrux|LINK
I have a View. Inside view - PartialView. If I call Partial with the name and new ViewDataDictionary, based on new data - have error:
The model item passed into the is of type ViewDataDictionary MyViewModelType, but this ViewDataDictionary instance requires a model item of type MyPartialViewModelType.
If instead of the ViewData to give the object, error is gone.
Task and use: I use EditorForModel, so need write custom object.cshtml; For all properties object, need call valid editor template (string, bool, etc). I get from the model modelmetadata properties, and by type, call partialview with valid template.
What's my problem?
PartialView (sample for string) http://pastebin.com/iuP7ukds
Dec 08, 2016 12:13 PM|Dmitry Sikorsky|LINK
When you pass nothing as view model to the partial view, partial view has the view model of the main view.
If you need to pass some data to the partial view model it would be good idea to build your main view model using the smaller view models.
For example, if you are building calendar you may pass calendar view model to the main view, and it may contain day view models for the partials of the days.
Please take a look at the example:
(But keep in mind that ViewModelBuilders should be renamed with ViewModelFactories, and method Build with method Create.)
Dec 08, 2016 12:25 PM|FJCrux|LINK
In your example, DayViewModel, doesn't have some metadata, it's just object with some properties, so you are just put model in partialview, not viewdatadictionary.
But in my case - metadata is really important. If I only need values, then I would not have puzzled over viewdatavictionary.
p.s. sorry for my bad english