我可以从子局部视图访问父视图的模型吗? [英] Can I access the parent view's Model from a child partial view?

查看:28
本文介绍了我可以从子局部视图访问父视图的模型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从子局部视图访问父视图的模型吗?我在查找时得​​到了相互矛盾的信息.

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?

推荐答案

否.如果您正在谈论将视图与 ViewModels 强绑定,那么您只能访问传递给局部视图的模型.但是如果您想要某些东西要在局部视图或任何其他视图中可用,您可以使用 ViewData 字典.

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.

这里的另一点是确保您将域模型与要绑定到视图的视图模型分开,而不是仅仅为了将其绑定到视图而对域模型进行 UI 相关更改.

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.

这篇关于我可以从子局部视图访问父视图的模型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆