I have a table with inputs inside the table. At the last column of each row have save buttons. Each time user click the save button, it will save the respective column. Here is my code:
<form action="@Url.Action("UpdateStaff", "Staff")" id="frmUpdateStaff" method="post">
<div class="table-responsive">
<table class="table table-hover table-sm table-striped">
<thead>
<tr>
<th>
No
</th>
<th>
StaffNo
</th>
<th>
Shift
</th>
<th>
</th>
</tr>
</thead>
<tbody>
@if (Model.Count() > 0)
{
var i = 0;
foreach (var item in Model)
{
i += 1;
<tr>
<td>@i</td>
<td>
@Html.DisplayFor(modelItem => item.StaffNo)
</td>
<td>
<select class="form-control input-sm" id="StaffShift" name="StaffShift" value="@item.StaffShift">
<option value="0" @(item.StaffShift== HMS.Domains.StaffShift.Morning? "selected" : "")>Morning</option>
@*<option value="1" @(item.StaffShift== HMS.Domains.StaffShift.Evening? "selected" : "")>Evening</option>*@
</select>
</td>
<td>
<input type="text" class="form-control input-sm" name="StaffNo" id="StaffNo" value=@item.StaffNo>
<button type='submit' class='btn btn-primary btn-xs' data-toggle="tooltip" title="Save"><span class="glyphicon glyphicon-floppy-disk"></span></button>
</td>
</tr>
}
}
else
{
<tr>
<td colspan="12"><div align="center">No records found</div></td>
</tr>
}
</tbody>
</table>
</div>
</form>
I created input named StaffNo
to store the primary key of the model. When the user click save
button, it should send the respective StaffNo
. It actually show the correct value in the view. But when I save the row, the StaffNo
always have value 1
in the controller. It actually take the the first row of data.