How can I get the ID of checked rows of a table in my View? ASP.NET Core 5 MVC

Viewed 183

In my view I have a table like this one:

<div class="table-wrapper-scroll-y table-scroll tableAreas">
    <table id="tableOp" class="table table-hover table-responsive-sm display">
        <thead>
            <tr class="thead table-dark">
                <th name="Checked"></th>
                <th class="theader " scope="col">
                    <p>Employee</p>
                </th>
                <th class="theader searchable" scope="col" name="Name">
                    <p>Name</p>
                </th>
                <th class="theader searchable" scope="col" name="RevEmp">
                    <p>Rev Employee</p>
                </th>
            </tr>
        </thead>

        <tbody>
            @foreach (OjtPmapsxemp pxEmpItem in (List<OjtPmapsxemp>)ViewBag.PMAPEmp)
            {
                <tr class="table-light">
                    <td><input type="checkbox" /></td>
                    <td>@pxempItem.Empid</td>
                    <td>@pxempItem.Name</td>
                    <td>@pxempItem.Revision</td>
                </tr>
            }
        </tbody>
    </table>
</div>

This throws me a table filled with a series of rows with a checkbox and other three fields, I want to be able to retrieve the Empids only of the rows that are checked with the checkbox, but I don't really know how to start, because having an attribute of type "name" within my input will cause that I'll only get the first ID

1 Answers

In your model, OjtPmapsxemp instance, add a boolean Property called IsSelected. Bind that Property to the checkbox in the View. Something like Once you submit the data back to the Post Method, you can filter the model list to get the selected items by IsSelected

Related