在我的 ViewModel 中创建与视图层次结构相对应的层次结构

发布于 2024-10-04 13:12:15 字数 771 浏览 0 评论 0原文

我正在 ASP.NET 中创建一个页面。在此页面上,有几个部分是我使用部分视图概括的。例如,我有一个配对页面,其中包含页面上两个人的信息,并且我使用人员部分视图进行概括。

当我在控制器中的操作中使用此数据时,我使用 PairViewModel 来表示我的配对视图上的数据。我想说,我用 PersonViewModel 代表每个人的部分视图。我的 PairViewModel 将有两个对两个 PersonViewModel 的引用。

我必须以某种方式将 PairViewModel 中的 PersonViewModels 与配对视图中的部分视图进行映射。我将如何指定这一点?

当我执行后期操作时也是如此。在控制器中,我将定义一个函数框架

public ActionResult MyAction(PairViewModel pair)

通过我在视图中指定的映射或通过查看成员名称自动将视图的值传输到PairViewModel(对此不是100%确定)视图上的字段名称。它如何将部分视图的值映射到 PairViewModel 中的 PersonViewModel 实例?

背景: 我是 ASP.NET 和 ViewModel 的新手。我之前在 WPF 中使用过一些 ViewModel,并尝试在其中添加一些层次结构。我最终的表现还不错,但在某些方面仍然显得有点混乱。至于我使用 ASP.NET 的经验,我刚刚完成了书呆子晚餐教程,仅此而已:)。

提前致谢。

I am creating a page in ASP.NET. On this page there are a few parts which I generalize using partial views. For example, I have a pair page with information about two persons on the page and I generalize using a person partial view.

When I am using this data in my controller on an action I use a PairViewModel to represent the data on my pair view. I would say, I represent each person partial view with a PersonViewModel. My PairViewModel will have two references to two PersonViewModels.

I have to somehow map the PersonViewModels in the PairViewModel with the partial views in the pair view. How would I specify this?

Also when I perform a post action. In the controller I would define a function

public ActionResult MyAction(PairViewModel pair)

The framework transfers the values of the view to the PairViewModel (not 100% sure about this) by the mapping I specified in view or automatically by looking at the member names and names of fields on the view. How would it map the values of the partial views onto the PersonViewModel instances in the PairViewModel?

Background:
I am new to ASP.NET and the use of ViewModels. I have used ViewModels a bit in WPF before and tried to put some hierarchy in them as well. What I go in the end was quite ok, but still seemed a bit messy on some points. As for my experience with ASP.NET, I just finished the Nerd Dinner tutorial and that's about it :).

Thanks in advance.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文