Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I access the parent view's Model from a child partial view?

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?

like image 614
kpollock Avatar asked Nov 05 '14 08:11

kpollock


People also ask

How do I access partial view 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.

Can we use model in partial view?

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.

Can partial view have controller?

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.


1 Answers

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.

like image 171
Jags Avatar answered Sep 28 '22 02:09

Jags