Can I access the parent view's Model from a child partial view? I get conflicting information when I look this up.
If so, can anyone point me at an example?
And if so, would it help to make the child model object a property of the parent model?
To create a partial view, right click on Shared folder -> select Add -> click on View.. Note: If the partial view will be shared with multiple views, then create it in the Shared folder; otherwise you can create the partial view in the same folder where it is going to be used.
Partial Views can use the Page Model for their data whereas Child Actions use independent data from the Controller. Editor/Display templates pass items from the model to the system but can be overridden by user partial views.
It does not require to have a controller action method to call it. Partial view data is dependent of parent model. Caching is not possible as it is tightly bound with parent view (controller action method) and parent's model.
No.If you are talking about strongly binding the views with ViewModels then you can only access the model which you pass to your partial view.But if you want something to be available in partial view or any other view you can use ViewData dictionary.
Other thing you can always do and should do is to make your view model in such a way so that it caters to your parent view as well as your all child views .So to answer your last question yes you can make child model object a property of your parent model.
Another point here is that make sure you keep your domain model separate from View model which you are going to bind to Views and not make UI related changes to your domain model for just the sake of binding it to view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With