ASP.NET MVC2 - 是否可以从部分视图访问父视图的模型数据?
在处理 1--->0...1 关系时,我尝试对 0...1 端使用单独的部分视图。为了提高效率,我想使用 RenderPartial() 而不是 RenderAction()。
是否可以从该部分视图访问包含视图的模型数据,以便访问主对象的 PK/ID?
这是否只是一次可悲的黑客尝试,根本就不应该考虑?
有谁有更好的例子来说明如何使用 MVC 处理这个 1--->0...1 关系?
In handling a 1--->0...1 relationship, I'm trying to use a separate partial view for the 0...1 end. I'd like to use RenderPartial() rather than RenderAction(), for the sake of efficiency.
Is it possible to gain access to the containing view's model data from this partial view, in order to access the PK/ID of the main object?
Is this just a sad attempt at a hack that shouldn't even be considered in the first place?
Does anyone have a better example of how to handle this 1--->0...1 relationship using MVC?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有点像。
如果您不将模型传递给 RenderPartial,则默认情况下会传递父视图。因此,您可以通过部分的
Model
属性来访问它。但如果你确实传递了一个模型,那么不,部分模型看不到父模型,因为它看到的是自己的模型。
我会说“kludge”而不是“hack”,但是是的,可能是这样。 :)
Sort of.
If you don't pass a model to
RenderPartial
, the parent's view is passed by default. So you can access it via the partial'sModel
property.But if you do pass a model, then no, the partial can't see the parent's model, because it sees its own instead.
I'd say "kludge" rather than "hack", but yes, probably. :)
首先问为什么需要PK?
但是,如果我确实需要的话,我会在子模型中拥有 ParentID 属性。然后你只需在发送之前设置它即可。
如果您需要父级的所有数据,那么您可以使用父属性并设置整个属性。
这种逻辑更适合在模型本身中,但是像这样:
或类似的东西。这实际上取决于事情的设置方式,但这是总体思路。
First ask why you need the PK?
However I'd have a ParentID property in the child model if I really needed to have it. Then you just set it before you send it off.
If you needed ALL of the data from the parent then you can have a Parent Property instead and set the whole property.
This logic would be better suited to be in the Model itself however like this:
or something of the sort. It would really depend on how things are set up already, but that's the general idea.