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

前端 未结 5 618
迷失自我
迷失自我 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:02

    Use a custom Model Binder, and write the Action methods as you would normally:

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

    In your ProductModelBinder, you iterate over the Form Collection values and bind to a Product entity. This keeps the Controller interface intuitive, and can help testing.

    class ProductModelBinder : IModelBinder ...
    

提交回复
热议问题