【问题标题】:ASP.NET MVC Editing A Collection Best Practices - Your OpinionASP.NET MVC 编辑集合最佳实践 - 您的意见
【发布时间】:2008-11-14 02:30:01
【问题描述】:

鉴于以下课程,您对处理创建/编辑的最佳方式有何看法,其中 Attributes.Count 可以是任意数字。

public class Product {
  public int Id {get;set;}
  public string Name {get;set;}
  public IList<Attribute> Attributes {get;set;}
}

public class Attribute {
  public string Name {get;set;}
  public string Value {get;set;}
}

用户应该能够在同一视图中编辑产品详细信息(名称)和属性详细信息(名称/值),包括添加和删除新属性。

处理模型中的更改很容易,处理 UI 和 ActionMethod 方面的最佳方法是什么?

【问题讨论】:

    标签: asp.net-mvc collections


    【解决方案1】:

    查看 Steve Sanderson 的博文 Editing a variable length list, ASP.NET MVC 2-style

    控制器

    您的操作方法接收您的本机域模型Product 并保持非常简单:

    public ActionResult Edit(Product model)
    

    查看

    Edit.aspx

    <!-- Your Product inputs -->
    <!-- ... -->
    
    <!-- Attributes collection edit -->
    <% foreach (Attribute attr in Model.Attributes)
       {
           Html.RenderPartial("AttributeEditRow", attr);
       } %>
    

    AttributeEditRow.ascx

    注意助手扩展Html.BeginCollectionItem(string)

    <% using(Html.BeginCollectionItem("Attributes")) { %>
        <!-- Your Attribute inputs -->
    <% } %>
    

    也可以添加和编辑新属性。见帖子。

    【讨论】:

      【解决方案2】:

      使用 FormCollection 并遍历键/值对。大概您可以使用一种命名方案,该方案允许您确定哪些键/值对属于您的属性集。

      [AcceptVerbs( HttpVerb.POST )]
      public ActionResult Whatever( FormCollection form )
      {
       ....
      }
      

      【讨论】:

        【解决方案3】:

        使用自定义模型绑定器,并像往常一样编写操作方法:

        ActionResult Edit(
          int id, 
          [ModelBinder(typeof(ProductModelBinder))] Product product
        ) ...
        

        在您的 ProductModelBinder 中,您遍历 Form Collection 值并绑定到 Product 实体。这使 Controller 界面保持直观,并有助于测试。

        class ProductModelBinder : IModelBinder ...
        

        【讨论】:

          【解决方案4】:

          取决于您希望为用户创建的体验。我已经为标记内容实现了类似的东西。在模型中,标签表示为 IList,但 UI 在单个文本字段中显示逗号分隔的列表。然后我处理将列表中的项目合并为一个字符串以填充文本字段,然后我拆分输入以将项目放回模型中的 IList 中。

          在我的 DAL 中,然后我处理将列表转换为 LINQ 实体、处理插入和删除等。

          这不是最直接的代码,但管理起来并不难,它为用户提供了预期的界面。

          我确信还有其他方法可以处理它,但我会专注于最适合用户的方法,然后根据它制定映射细节。

          【讨论】:

            【解决方案5】:

            安德鲁,

            我在想一些比标签更难的东西。在这个简单的例子中,名称/值对 .. color: Red;尺寸:10;材质:棉。

            我认为任何可以使用的东西都可以扩展到更复杂的地方。 IE。添加类别并在同一页面上添加其所有项目。使用一些 jQuery 添加另一行相对容易,但是将信息发送到 ActionMethod 的共识是什么?

            你不会编码:

            public ActionResult Whatever(stirng attr1Name, string attr2Name, string attr3Name ...
            

            我也不认为接受这个会起作用:

            public ActionResult Whatever(ILIst<Attribute> attributes, string productName ...
            

            【讨论】:

            • 你是对的。我没有考虑对象的集合......我的大脑卡在我当前的项目上。我想 tvanfosson 的回复中提到的 FormCollection 是最好的选择。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-03-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-09-06
            • 1970-01-01
            • 2023-03-12
            相关资源
            最近更新 更多