简体   繁体   English

使用 PageModel 将 HTML 表信息从 cshtml 传递到 onpost cshtml.cs 方法

[英]Passing HTML table information from cshtml to method onpost cshtml.cs using PageModel

I'm using asp.net core and bootstrap, I'm trying to update my table when I check and uncheck the checkboxes.我正在使用 asp.net 核心和引导程序,当我选中和取消选中复选框时,我正在尝试更新我的表。 The problem is that when I click on button save to update the table I want to receive all the information, but when I try to do that somehow my IList<OutputAccessRights> userAccessRights returns Count = 0. How can I post-back without losing everything?问题是,当我单击按钮保存更新表时,我想接收所有信息,但是当我尝试以某种方式执行此操作时,我的IList<OutputAccessRights> userAccessRights返回 Count = 0。如何在不丢失所有内容的情况IList<OutputAccessRights> userAccessRights传? Can I do using jquery, javascript?我可以使用 jquery、javascript 吗?

.cs: 。CS:

public IActionResult OnPost(int id, string groupAccessName, bool chkDefaultGroup, IList<OutputAccessRights> userAccessRights, string returnUrl = null)
        {
            ReturnUrl = returnUrl;

                else //Update
                {
                    Security security = new Security();
                    security.GroupAccessUpdate(BellaMain.GlobalVariable.SystemID, Convert.ToInt16(groupAccessID), groupAccessName, false);
                    Update(Convert.ToInt16(groupAccessID), userAccessRights);
                    GroupAccessID = id;
                    GroupAccessName = groupAccessName;

                    return RedirectToAction("Group AccessDetails", "Form", new { id = GroupAccessID, searchString = SearchString, searchInt = SearchInt }).WithSuccess("Success!", "Updated item!");
                }

            return Page();
        }

Model:模型:

 public class GroupAccessDetailsModel : PageModel
    {
        private readonly ILogger<GroupAccessDetailsModel> _logger;

        public GroupAccessDetailsModel(ILogger<GroupAccessDetailsModel> logger)
        {
            _logger = logger;
        }
        public class OutputAccessRights
        {
            public byte MainMenuID { get; set; }
            public byte SubMenuID { get; set; }
            public byte OperationID { get; set; }
            public string MainMenuDescription { get; set; }
            public string SubMenuDescription { get; set; }
            public string Operation { get; set; }
            public bool ChkUserAccessRights { get; set; }
            public bool ChkAddRight { get; set; }
            public bool ChkUpdateRight { get; set; }
            public bool ChkDelete { get; set; }
            public bool FlagDefaultGroupAlreadySet { get; set; }
        }
        [BindProperty]
        public IList<OutputAccessRights> UsersAccessRights { get; set; }
}

html: html:

 @if (Model.UsersAccessRights != null)
{
    <table class="table table-striped table-bordered dataTable tableAccessRights" name="userAccessRights" id="userAccessRights" style="width:100%">
        <thead>
            <tr>
                <th>
                    MainMenu
                </th>
                <th>
                    SubMenu
                </th>
                <th>
                    Operation
                </th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.UsersAccessRights)
            {
                <tr>
                    <td>
                        @if (Model.GroupAccessID == 0)
                        {
                            <input type="checkbox" class="form-check-inline" name="@item.ChkUserAccessRights" id="chkUserAccessRights" asp-for="@item.ChkUserAccessRights" />
                            @Html.DisplayFor(modelItem => item.MainMenuDescription)
                        }
                        else
                        {
                            @if (Model.Details != true)
                            {
                                <input type="checkbox" class="form-check-inline" name="@item.ChkUserAccessRights" id="chkUserAccessRights" asp-for="@item.ChkUserAccessRights" />
                                @Html.DisplayFor(modelItem => item.MainMenuDescription)
                                <span class="text-danger"></span>
                            }
                            else
                            {
                                <input type="checkbox" class="form-check-inline" name="@item.ChkUserAccessRights" id="chkUserAccessRights" disabled readonly="readonly" />
                                @Html.DisplayFor(modelItem => item.MainMenuDescription)
                            }
                        }
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.SubMenuDescription)
                    </td>
                    <td>
                        @if (Model.GroupAccessID == 0)
                        {
                            <input type="checkbox" class="form-check-inline" name="@item.ChkAddRight" id="chkAddRight" asp-for="@item.ChkAddRight" />
                            <label for="chkAddRight">Insert</label>
                        }
                        else
                        {
                            @if (Model.Details != true)
                            {
                                <input type="checkbox" class="form-check-inline" name="@item.ChkAddRight" id="chkAddRight" asp-for="@item.ChkAddRight" />
                                <label for="chkAddRight">Insert</label>
                                <span class="text-danger"></span>
                            }
                            else
                            {
                                <input type="checkbox" class="form-check-inline" name="@item.ChkAddRight" id="chkAddRight" disabled readonly="readonly" asp-for="@item.ChkAddRight" />
                                <label for="chkAddRight">Insert</label>
                            }
                        }
                        @if (Model.GroupAccessID == 0)
                        {
                            <input type="checkbox" class="form-check-inline" name="@item.ChkDelete" id="chkDelete" asp-for="@item.ChkDelete" />
                            <label for="chkDelete">Delete</label>
                        }
                        else
                        {
                            @if (Model.Details != true)
                            {
                                <input type="checkbox" class="form-check-inline" name="@item.ChkDelete" id="chkDelete" asp-for="@item.ChkDelete" />
                                <label for="chkDelete">Delete</label>
                                <span class="text-danger"></span>
                            }
                            else
                            {
                                <input type="checkbox" class="form-check-inline" name="@item.ChkDelete" id="chkDelete" disabled readonly="readonly" asp-for="@item.ChkDelete" />
                                <label for="chkDelete">Delete</label>
                            }
                        }
                        @if (Model.GroupAccessID == 0)
                        {
                            <input type="checkbox" class="form-check-inline" name="@item.ChkUpdateRight" id="chkUpdateRight" asp-for="@item.ChkUpdateRight" />
                            <label for="chkUpdateRight">Update</label>
                        }
                        else
                        {
                            @if (Model.Details != true)
                            {
                                <input type="checkbox" class="form-check-inline" name="@item.ChkUpdateRight" id="chkUpdateRight" asp-for="@item.ChkUpdateRight" />
                                <label for="chkUpdateRight">Update</label>
                                <span class="text-danger"></span>
                            }
                            else
                            {
                                <input type="checkbox" class="form-check-inline" name="@item.ChkUpdateRight" id="chkUpdateRight" disabled readonly="readonly" asp-for="@item.ChkUpdateRight" />
                                <label for="chkUpdateRight">Update</label>
                            }
                        }
                    </td>
                </tr>
            }

        </tbody>
    </table><button type="submit" class="btn btn-primary" asp-page="Group AccessDetails" asp-route-userAccessRights="@Model.UsersAccessRights">@Localizer["Save"]</button>
}

The fundamentals of http posts mean that only form controls (input, select) are posted back to the server. http post 的基本原理意味着只有表单控件(输入、选择)被回发到服务器。

Anything that the user has changed should be posted back to the server, and integrated back into a brand new get request.用户更改的任何内容都应回发到服务器,并重新集成到全新的 get 请求中。

So the pattern is something like:所以模式是这样的:

  1. Load page (GET request, Load data from database and return in view model, bind to HTML)加载页面(GET请求,从数据库加载数据并在视图模型中返回,绑定到HTML)
  2. Submit form (POST request containing user input, bind to a view model, update stored data)提交表单(包含用户输入的 POST 请求、绑定到视图模型、更新存储的数据)
  3. Redirect to GET action (reload data from database)重定向到 GET 操作(从数据库重新加载数据)

There's no getting around the fact that after a POST / form submit, you have to re-get the data from wherever it comes.在 POST / 表单提交之后,您必须从任何地方重新获取数据,这是无法回避的事实。

If you're not saving the user data to a database, then you just need to re-integrate it.如果您没有将用户数据保存到数据库中,那么您只需要重新集成它。

Edit:编辑:

If you really wanted to post the entire state of the page, you would have to put everything into an input (of type hidden to hide from the user) in order to send it back to the server.如果您真的想发布页面的整个状态,则必须将所有内容放入输入(隐藏类型以对用户隐藏)中,以便将其发送回服务器。

Bear in mind that all data that comes back from an input is user input and cannot be trusted to be correct.请记住,从输入返回的所有数据都是用户输入,不能相信是正确的。

Edit 2:编辑2:

Simple example:简单的例子:

public ActionResult LoadPage()
{
  var viewModel = service.GetViewModel();
  return View(viewModel);
}

[HttpPost]
public ActionResult PostPage(MyViewModel postedViewModel)
{
  service.UpdateData(postedViewModel);

  // either:
  var freshData = service.GetViewModel();
  return View(freshData);

  // or:
  // this is my preferred method, as it means that pressing F5 will not resubmit the old page
  return RedirectToAction("GetData");
}

// what I mean by integrating the data back into the fresh data:
[HttpPost]
public ActionResult PostWithIntegration(MyViewModel postedData)
{
  var freshData = service.GetViewModel();

  freshData.Update(postedData);

  return View(freshData);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM