How to pass List from view to controller - MVC 4

后端 未结 1 1289
面向向阳花
面向向阳花 2020-12-17 00:41

I missed a little thing while passing a list from view to controller. It displays null in [HttpPost] method of controller. Anyone please guide that how can I ge

相关标签:
1条回答
  • 2020-12-17 00:49

    With a foreach loop, MVC just creates the same inputs over and over again, because it doesn't know that they should be different. Therefore you need to use for-loops, and use indexes. I don't see any inputs in your form, so I'll just give you an example:

    @if (Model != null) {
        for (var i = 0; i < Model.Count; i++) {
            @Html.TextBoxFor(m => Model[i].SomeProperty)
        }
    }
    

    Also, if I remember correctly, you need to use an IList as model:

    @model IList<payorder_draft_printing.Models.registration>
    

    To be able to post your (non-editable) texts, you need to add hidden inputs:

    @Model[i].account_number 
    @Html.HiddenFor(m => Model[i].account_number)
    
    0 讨论(0)
提交回复
热议问题