MVC .NET Model Binding to Array on the fly

后端 未结 2 1709
不知归路
不知归路 2020-12-09 22:56

In Scotts blog post he describes how to post an array of objects to the controller.

My Question how best to generate a View for this that allows th

相关标签:
2条回答
  • 2020-12-09 23:23

    You may see This In this post steve Validated a variable length list in which textboxes may be added or deleted dynamically

    0 讨论(0)
  • 2020-12-09 23:25

    when rendering and binding to lists or arrays, i normally prefer for loop over foreach loop because it will generate required indices for the modelbinder to bind data to the model.

    for(int i=0;i<Model.Count(); i++)
    {
       <%:Html.TextBox("Items["+i+"].Name", Model[i].Name)%>
    }
    

    i m not comfortable with strongly typed helpers when it comes to list binding but its purely a matter of personal preference. However, if you are allowing the user to dynamically add and remove items from page through javascript you have to take care of indices yourself.

    EDIT: i have blogged about creating master detail form in asp.net mvc using jquery templates which is relevant in list binding scenario as well

    0 讨论(0)
提交回复
热议问题