0

I have the following table:

<form method="post" asp-action="Index" asp-controller="Record">
    <table class="table table-hover">
        <thead>
            <tr>
                <th scope="col"><input type="checkbox" id="chkAll"></th>
                <th scope="col">Created</th>
                <th scope="col">User Reference</th>
                <th scope="col">Type</th>
                <th scope="col">Status</th>
                <th>Action</th>

            </tr>
        </thead>
        <tbody>
            @foreach (var rec in Model.Data)
            {
                <tr>
                    <td style="display: none">
                        @Html.TextBoxFor(m => rec.Id)
                    </td>
                    <td>
                        @Html.CheckBoxFor(m => rec.IsChecked)
                    </td>
                    <td>@rec.Created</td>
                    <td>@rec.UserReference</td>
                    <td>@rec.Type</td>
                    <td>@rec.Status</td>
                    <td>
                        <input name="submit" type="submit" id="process" value="submit" />
                    </td>
                </tr>
            }
        </tbody>
    </table>
</form>

The issue I have is when I press submit I'm expecting to pass the content of the table into my controller, however when I submit the form the model on my controller is null:

[HttpPost]
public async Task<IActionResult> Index(List<UserData> data)

When I submit the form I expect to have a list of id's and checkbox values either true / false, can someone shed some light into why I'm not able to see this within the post method of the controller?

Code Ratchet
  • 5,758
  • 18
  • 77
  • 141
  • The solution with a good explanation can be found [here](https://stackoverflow.com/questions/19964553/mvc-form-not-able-to-post-list-of-objects). – Luis Jul 30 '20 at 03:13

1 Answers1

0

Found the solution of the following thread, seems switching foreach to for resolved the issue.

Code Ratchet
  • 5,758
  • 18
  • 77
  • 141
  • No, it is not because of the use of `for` instead of `foreach`, it is because the `name` generated includes information about object's index. This `@Html.DisplayFor(modelItem => Model[i].Id)` generates HTML like this ``. – Luis Jul 30 '20 at 03:07