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

Javascript 使用PageModel将HTML表信息从cshtml传递到方法onpost cshtml.cs,javascript,c#,jquery,asp.net,post,Javascript,C#,Jquery,Asp.net,Post,我正在使用asp.net内核和引导程序,当我选中和取消选中复选框时,我正在尝试更新我的表。问题是,当我单击按钮save更新表时,我希望接收所有信息,但当我尝试这样做时,我的IList userAccessRights返回Count=0。我怎样才能在不丢失一切的情况下发回?我可以使用jquery吗, javascript .cs: public IActionResult OnPost(int-id,string-groupAccessName,bool-chkDefaultGroup,IList

我正在使用asp.net内核和引导程序,当我选中和取消选中复选框时,我正在尝试更新我的表。问题是,当我单击按钮save更新表时,我希望接收所有信息,但当我尝试这样做时,我的
IList userAccessRights
返回Count=0。我怎样才能在不丢失一切的情况下发回?我可以使用jquery吗, javascript

.cs:

public IActionResult OnPost(int-id,string-groupAccessName,bool-chkDefaultGroup,IList-userAccessRights,string-returnUrl=null)
{
ReturnUrl=ReturnUrl;
else//更新
{
安全性=新安全性();
security.GroupAccessUpdate(BellaMain.GlobalVariable.SystemID,Convert.ToInt16(groupAccessID),groupAccessName,false);
更新(Convert.ToInt16(groupAccessID),userAccessRights);
GroupAccessID=id;
GroupAccessName=GroupAccessName;
返回RedirectToAction(“组访问详细信息”,“表单”,新建{id=GroupAccessID,searchString=searchString,searchInt=searchInt});
}
返回页();
}
型号:

 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; }
}
公共类GroupAccessDetailsModel:PageModel
{
专用只读ILogger\u记录器;
公共组访问详细信息模型(ILogger记录器)
{
_记录器=记录器;
}
公共类OutputAccessRights
{
公共字节MainMenuID{get;set;}
公共字节子菜单ID{get;set;}
公共字节操作ID{get;set;}
公共字符串MainMenuDescription{get;set;}
公共字符串子菜单说明{get;set;}
公共字符串操作{get;set;}
公共bool ChkUserAccessRights{get;set;}
公共bool ChkAddRight{get;set;}
公共bool ChkUpdateRight{get;set;}
公共bool ChkDelete{get;set;}
public bool FlagDefaultGroupAlreadySet{get;set;}
}
[BindProperty]
公共IList UsersAccessRights{get;set;}
}
html:

@if(Model.UsersAccessRights!=null)
{
主菜单
子菜单
活动
@foreach(Model.UsersAccessRights中的var项)
{
@if(Model.GroupAccessID==0)
{
@DisplayFor(modelItem=>item.MainMenuDescription)
}
其他的
{
@如果(Model.Details!=true)
{
@DisplayFor(modelItem=>item.MainMenuDescription)
}
其他的
{
@DisplayFor(modelItem=>item.MainMenuDescription)
}
}
@DisplayFor(modelItem=>item.SubnumDescription)
@if(Model.GroupAccessID==0)
{
插入
}
其他的
{
@如果(Model.Details!=true)
{
插入
}
其他的
{
插入
}
}
@if(Model.GroupAccessID==0)
{
删除
}
其他的
{
@如果(Model.Details!=true)
{
删除
}
其他的
{
删除
}
}
@if(Model.GroupAccessID==0)
{
更新
}
其他的
{
@如果(Model.Details!=true)
{
更新
}
其他的
{
更新
}
}
}
@定位器[“保存”]
}

http POST的基本原理意味着只有表单控件(输入、选择)被发回服务器

用户所做的任何更改都应该发布回服务器,并集成回一个全新的get请求中

因此,模式类似于:

  • 加载页面(获取请求,Lo
     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; }
    }
    
     @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>
    }
    
    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);
    }