Sarah
Sarah

Reputation: 329

MVC Core how to use checkboxes to retrieve the foreach ID

sorry if my title is very unclear.

So I have this following code:

                @{
                    foreach (var m in Model.AsEnumerable())
                    {
                        <tr class="oddeventable">

                            <td>
                            <Student Name>
                            </td>
                            <td>
                                @Html.ActionLink("Delete", "DeleteStudent", new { stud = m.Id }, new {@class="btn btn-danger" })
                            </td>
                        </tr>

                        i++;
                    }
                }

              <input type="submit"/>

How do I change the @Html.ActionLink("Delete", "DeleteStudent", new { stud = m.Id }, new {@class="btn btn-danger" }) Into a checkboxe so when the foreach is being run, there will be many rows with the checkbox and the user can select each rows then click "Submit", when submit to the controller, I want to retrieve the M.id so I can do the appropriate action.

Thanks a lot.

Upvotes: 1

Views: 347

Answers (1)

Planet-Zoom
Planet-Zoom

Reputation: 1185

first use For loop instead of Foreach , then you can use @Html.CheckBoxFor(m =>m[index].Id}); this will generate proper elements with name and ids. in form collection at server side you can access these values using index number

Upvotes: 1

Related Questions