ASP.NET MVC Editing A Collection Best Practices - Your Opinion

前端 未结 5 620
迷失自我
迷失自我 2020-12-29 10:23

Given the following class, what is your opinion on the best way to handle create/edit where Attributes.Count can be any number.

public class Product {
  pub         


        
5条回答
  •  南笙
    南笙 (楼主)
    2020-12-29 11:19

    Look at Steve Sanderson’s blog post Editing a variable length list, ASP.NET MVC 2-style.

    Controller

    Your action method receives your native domain model Product and stays pretty simple:

    public ActionResult Edit(Product model)
    

    View

    Edit.aspx

    
    
    
    
    <% foreach (Attribute attr in Model.Attributes)
       {
           Html.RenderPartial("AttributeEditRow", attr);
       } %>
    

    AttributeEditRow.ascx

    Pay your attention to helper extension Html.BeginCollectionItem(string)

    <% using(Html.BeginCollectionItem("Attributes")) { %>
        
    <% } %>
    

    Adding and editing of new attributes is possible too. See the post.

提交回复
热议问题