简体   繁体   English

从视图的提交按钮调用 post 方法后,如何保留表的选定行

[英]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);
                }

            });
        });

result:结果: 在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DatePicker-如何在单击提交按钮后保持选定的值 - DatePicker - how to keep the selected value after clicking on submit button 如何将表格中的选定行作为表单提交中的请求参数传递? - How to pass selected rows from a table as request parameters on form submit? 在单击“提交”按钮后,如何保持关闭登录模式? - How do I keep a login modal from closing after the submit button is hit? 单击按钮提交后,如何将下拉列表中的选定值传递给url查询字符串? - How can I pass the selected values from the dropdown list to the url query string when button submit is clicked? 按下提交按钮后,如何阻止单选按钮取消选中? - How can I stop a radio button from unchecking after I push the submit button? 提交后如何保持模式对话框? - How can I keep a modal dialog after a submit? Wordpress:如何在提交后保留searchform中的市场复选框? - Wordpress: How can I keep market checkboxes in searchform after submit? 从javascript提交表单后,如何通过提交按钮保持php $ _GET值? - How to keep php $_GET value from a submit button after form submit from javascript? 单击后禁用提交按钮,并保持POST / REDIRECT / GET工作 - Disable submit button after click and keep POST/REDIRECT/GET working 当管理员选择批准按钮并单击提交按钮时以及刷新后,如何禁用表行中的按钮? - How to disable button in table rows when admins select approve button and click submit button, and also after refreshing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM