[英]How can I keep the selected rows of the table after calling the post method from submit button of a view
After calling the post method from the controller, the selected rows of the table is disappeared.从 controller 调用 post 方法后,表格的选定行消失了。 I am trying to keep the selected rows as it is even after post method ReinstateEmployee() calling from the server.
我试图保留选定的行,即使在从服务器调用 post 方法 ReinstateEmployee() 之后也是如此。 I am looking for someone help.
我正在找人帮忙。
Here is the code这是代码
Model class I am keeping one more attribute to keep selected record Model class 我还保留了一个属性来保留选定的记录
public class EmployeeReinstateVM
{
public List<string> selectedEmployeeId { get; set; }
}
Controller Controller
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult ReinstateEmployee(string btnsubmit, string btnActivate, EmployeeReinstateVM model)
{
var selectedEmployeeId = model.selectedEmployeeId;
return view()
}
View file查看文件
<style>
.selectable-row.selected {
background-color: #ddd;
}
</style>
<table class="table table-bordered table-light">
<tbody>
foreach (var item in Model.employees)
{
<tr class="selectable-row" employee-id="@item.EmployeeID">
<td>@item.EmployeeID</td>
<td>@item.DepotName</td>
<td>@item.EmployeeName</td>
<td>@item.EmpLeaveDate</td>
</tr>
}
</tbody>
</table>
<input type="hidden" id="selectedEmployeeId" name="selectedEmployeeId" value="">
<button type="submit" class="btn btn-primary form-control" id="btnSave" name="btnActivate" value="update">
List Leavers
</button>
<script type="text/javascript">
$(document).ready(function () {
var employeeIds = [];
$(".selectable-row").click(function () {
$(this).toggleClass("selected");
var employeeId = $(this).attr('employee-id');
if ($(this).hasClass("selected")) {
employeeIds.push(employeeId);
}
else {
employeeIds = employeeIds.filter(function (id) {
return id !== employeeId;
});
}
});
$("#btnSave").click(function () {
$("#selectedEmployeeId").val(employeeIds);
console.log($("#selectedEmployeeId").val());
});
});
}
</script>
return view()
返回视图()
If we use this, we will refresh the view.如果我们使用它,我们将刷新视图。
I have a suggestion that you can use ajax to post your form, then we can keep the selected rows.我有一个建议,你可以使用ajax发布你的表格,然后我们可以保留选定的行。
I test your code, but I cannot change the row background-color, and I change the color.You can refer to it.我测试了你的代码,但是我不能改变行的背景颜色,我改变了颜色。你可以参考它。
In controller:在 controller:
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult ReinstateEmployee(string btnsubmit, string btnActivate, EmployeeReinstateVM model)
{
//var selectedEmployeeId = model.selectedEmployeeId;
//return View();
return Json(btnsubmit);
}
In the view:在视图中:
$("#btnSave").click(function () {
$("#selectedEmployeeId").val(employeeIds);
console.log($("#selectedEmployeeId").val());
$.ajax({
type: "POST",
url: "/Keepselected/ReinstateEmployee",
data: { "btnsubmit": "aa" },
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
success: function (response) {
alert(response);
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.