How to bind a List<Foo> Property to a model so it can be transferred from view to the controller endpoint?

29 Views Asked by At

I am showcasing a list that has a type of a class that I created. The area where I display that list is incased in a @Ajax.BeginForm. I want to send the data inside the list that is within the model that the view has access to to a controller endpoint.

How can I properly bind the list property?

I have tried placing an @Html.HiddenFor(m => m.MyList[i].ID) inside the for loop where I display everything. It connects to the correct endpoint but the data inside it is null.

My form:

@using (Ajax.BeginForm("RemarkMaterialsRemove", "Operation",
            new AjaxOptions()
                {
                    HttpMethod = "POST",
                    OnBegin = "OnRemovingMaterialBegin",
                    OnSuccess = "OnRemovingMaterialSuccess"
                }, new { @id = "materialForm"}
        ))
        {
            <div class="form-floating mt-2">
                <div class="content-block overflow-auto w-100 p-2" id="partList">
                    <table id="tblParts" class="display text-nowrap">
                        <thead>
                            <tr>
                                <th class="text-center"><a id="deleteAllMaterial"><i class="fa fa-trash"></a></th>
                                <th style="min-width:200px">Material</th>
                                <th style="min-width:200px">Description</th>
                            </tr>
                        </thead>
                        <tbody>
                            @if (Model.MaterialData != null && Model.MaterialData.Count() > 0)
                            {
                                for (int i = 0; i < Model.MaterialData.Count(); i++)
                                {
                                    <tr>
                                        <td class="icon-field"><a class="deletePart" data-attr="@Model.MaterialData[i].ID"><i class="fa fa-trash icon-action"></i></a></td>
                                        <td>@Model.MaterialData[i].Material</td>
                                        <td>@Model.MaterialData[i].Description</td>
                                    </tr>
                                }
                            }
                        </tbody>
                    </table>
                </div>
            </div>
        }

My endpoint:


        [HttpPost]
        public ActionResult RemarkMaterialsRemove(EditRequestData data)

What can I do?

0

There are 0 best solutions below