在 ASP.NET MVC2 中使用带有部分视图的模型的正确方法是什么?

发布于 2024-10-02 20:09:03 字数 333 浏览 1 评论 0原文

我对 MVC 还很陌生,但现在对这种模式感到非常满意,特别是发现 ViewModel 是控制器和视图之间的绝佳桥梁。

但是,我现在必须使用部分视图,并且有点困惑。

要求

好的,在线框架中我们有许多不同的页面,但它们共享一个共同的“网格”。

网格将是部分视图,并且将始终呈现/使用/操作相同类型的数据。

问题

因此,包括部分视图的主视图将具有不同的模型......那么我如何从部分视图中处理模型。

使用特定部分视图的视图的所有模型是否都应将部分视图的嵌套模型作为属性?

这里的最佳实践是什么?

酷,干杯

I'm pretty new to MVC, but now feel pretty comfortable with the pattern, particularly finding ViewModels to be an excellent bridge between controller and view.

However, I'm now going to have to use Partial Views, and am a little confused.

Requirement

Ok, in the Wire Frames we have many different pages, but which share a common "grid".

The Grid will be a Partial View and will always render/consume/manipulate the same TYPE of data.

Questions

So, the main Views that include the partial view will have different Models... so how do I handle the models from within the partial view.

Should all Models for Views that use that certain Partial View have a nested model for the partial view as a property?

What the best practice here?

cool, cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命硬 2024-10-09 20:09:03

我发现当我组合一个部分时,如果我考虑该部分的模型是什么,这会非常有帮助。我几乎总是发现,如果我花点时间考虑这一点,部分的可重用性会增加很多。

部分通常应该使用整个视图的模型中数据的一些自然子集或变体。当您在创建部分时就考虑到这一点,必须过多考虑“我的部分应该使用什么模型”通常表明您可能应该创建一个不同的部分而不是尝试重用一个部分。

如果您必须为 Partial 重新构建一个新模型,我认为问题可能是您的整个视图的模型没有涵盖您需要的所有内容。

话虽如此,但;很多时候,我有一个局部模型,它使用的模型似乎与整个视图的模型是分开的。但大多数情况下,这些模型是新的/空的对象 - 不是需要实例化并设置属性等的模型(这通常发生在 Ajax 相关更新中使用的 Partials 中)

请注意,MVC2 对此有一些东西不过,也是如此; Html.RenderAction() 允许您调用一个全新的 Action(它有自己的模型)并在当前视图中渲染它。实际上,我自己尽量避免使用它......但对我来说,这比尝试为部分“构建”一些新模型或在当前 ViewModel 中添加不自然的信息要好。

I find that when I am putting together a Partial, it helps immensely if I consider what the Model for that Partial will be. I almost always find that if I take a moment to consider that, the reusability of the partial increases a lot.

A Partial should usually use some natural sub-set or variation of the data in the Model of the whole View. When your creation of a Partial starts with that in mind, having to think too much about "what model should my partial use" is often an indication that perhaps you should be creating a different partial instead of trying to re-use one.

If you are having to re-construct a new model for a Partial, I think the problem may be that your whole View's model isn't covering everything you need.

With that said, though; there are many times where I have a Partial that uses a Model that seems to be separate from the whole View's Model. But most of the time those models are new/empty objects - not ones that need to be instantiated and have properties set, etc. (this usually happens for Partials that are being used in Ajax-related updates)

Note that MVC2 has something for this too, though; Html.RenderAction() lets you call a whole new Action (which would have its own Model) and render it within the current View. I try to avoid needing to use it, myself, actually... but it would feel better, to me, than trying to 'build' some new Model for a Partial, or adding in unnatural information in the current ViewModel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文