ASP.NET MVC 2:编辑嵌套实体的 C# 视图是什么样的?

发布于 2024-08-20 17:39:52 字数 1439 浏览 5 评论 0原文

在过去的几周里,我多次尝试让服务器端 MVC 2 视图来处理具有嵌套 IList 元素的对象,但没有取得太大成功。我缺少一些基本的理解,我希望这个问题能得到解决。

我想要的是一个显示产品列表的表单,您可以在其中在线更改信息,包括层次结构(每个产品都有一个子产品列表,每个产品都有一个图像列表等)我正在尝试重新创建一个旧的 MS Access 表单,其中大表单有一个产品列表,子表单显示相关产品,所有这些都允许内联编辑。 Access 在关注不同记录时保存每条记录。

假设您有一个如下所示的域模型:

public class Product {
    ... // Lots of fields like public string name {get; set;}
    public IList<Department> departments {get; set;}
    public IList<SubProduct> subProducts {get; set;}
}
public class SubProduct {
    ... // Lots of fields like public string name {get; set;}
    public IList<Image> images {get; set;}
}
public class Image {
    ... // Lots of fields like public string name {get; set;}
}

和一个如下所示的视图模型:

public class EditProduct {
    IList<Product> products {get; set;}
}

在 MVC 2 视图中,如何对编辑字段进行编码,以便可以将单个模型发布回控制器?我熟悉该

<input id="products[0].subProducts[0].images[0].name" /... >

机制,但每个 IList 都需要一种在同一屏幕上 CRUD 元素的方法。这是一个大数据输入屏幕,速度与能够查看列表中的所有产品一样重要。我需要添加按钮、删除按钮以及在编辑任何输入后发布整个表单的方法。

如果我从 JQuery 的角度来看:

  • 我将模型序列化为 JSON,作为前 N 个 IList 元素的初始形式,
  • 当其他产品到达容器 div 的底部时,我会动态加载同一列表中的其他产品
  • 我在保存/发布时接受整个模型,或者接受单个实体的相同表单的片段,例如为每个 IList 元素有一个单独的 html 表单标签,

但由于某种原因,我只是不知道如何在服务器端执行此操作。视图本身的任何链接或示例代码都很棒。没有 IList 的元素似乎效果很好,尤其是对于 UI 模板。我是否会在每个可编辑实体周围创建一堆单独的 html 表单标签,并且让帖子一次只处理一个元素?

I tried numerous times over the last few weeks to get a server side MVC 2 view to work with objects with nested IList elements without much success. I am missing some fundamental understanding that I hope this question resolves.

What I want is a form that shows a list of product where you can change information in-line, including a hierarchy (a list of product that each has a list of sub-products, that each has a list of images, etc.) I am trying to recreate an older MS Access form where the big form has a list of products and sub-forms show the related products, all allowing in-line edits. Access saves each record upon focusing on a different record.

Say you have a Domain Model that looks like this:

public class Product {
    ... // Lots of fields like public string name {get; set;}
    public IList<Department> departments {get; set;}
    public IList<SubProduct> subProducts {get; set;}
}
public class SubProduct {
    ... // Lots of fields like public string name {get; set;}
    public IList<Image> images {get; set;}
}
public class Image {
    ... // Lots of fields like public string name {get; set;}
}

And a view Model that looks like this:

public class EditProduct {
    IList<Product> products {get; set;}
}

In the MVC 2 View, how would you code the Edit fields so that you can post the single model back to the controller? I am familiar with the

<input id="products[0].subProducts[0].images[0].name" /... >

mechanic, but each IList needs a way to CRUD elements on the same screen. It's a big data entry screen where speed counts as much as being able to see all products in a list. I need Add buttons, Delete buttons, and a means to post the entire form after editing any inputs.

If I look at it from a JQuery point of view:

  • I serialize the Model as JSON for the initial form for the first N number of IList elements
  • I load on the fly additional products in the same list when they reach the bottom of the container div
  • I accept the entire Model upon save/post or accept pieces of the same form for individual entities, like having a separate html form tag for each IList element

But for some reason, I am just not getting how to do this at the server side. Any links or example code for the View itself would be great. Elements without IList seems to work great, especially with UI templates. Would I create a bunch of individual html form tags around each editable entity and have posts only deal with one element at a time?

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

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

发布评论

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

评论(1

乱了心跳 2024-08-27 17:39:52
<% for (int count = 0; count < Model.Students.Count; count++ )
   {                                              %><%= 
      Html.EditorFor(m => m.Students[count])      %><%
   } 
%>

创建表单名称,例如:

name="Students[0].Name"
name="Students[1].Name"
name="Students[2].Name"

绑定回原始列表视图模型

<% for (int count = 0; count < Model.Students.Count; count++ )
   {                                              %><%= 
      Html.EditorFor(m => m.Students[count])      %><%
   } 
%>

Creates form names like:

name="Students[0].Name"
name="Students[1].Name"
name="Students[2].Name"

Which bind back to the original List view model

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