IList returning count=0 on posting back updated checkboxes

给你一囗甜甜゛ 提交于 2020-06-01 06:46:05

问题


I'm using asp.net-core with razor pages. I'm trying to update the checkboxes in the table. When clicking on button save I pass with asp-route-for because I want to know how many items are in the list But my IList usersAccessRights is returning count = 0 and skips my foreach in update function. Is there other way to receive how many items and update the table checkboxes?

cshtml.cs:

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

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

private void Update(short GroupAccessID, IList<OutputAccessRights> usersAccessRights)
    {   Security Security = new Security();
        IDataReader dr;
        byte MainMenuId = 0;
        byte SubMenuId = 0;
        string Operation = "";
        string OperationId = "";
        foreach (var item in usersAccessRights)
        {
            MainMenuId = Convert.ToByte(item.MainMenuID);
            SubMenuId = Convert.ToByte(item.SubMenuID);
            //*** Add
            OperationId = "A";
            if (item.ChkAddRight == true)
                Operation = "ADD";
            else
                Operation = "REMOVE";
            Security.GroupAccessRightsMaintain(BellaMain.GlobalVariable.SystemID, Convert.ToInt16(GroupAccessID), MainMenuId, SubMenuId, OperationId, Operation);
            //*** Delete

cshtml - button save:

<div class="col-sm-4">
        @if (Model.Details != true)
        {
            <button type="submit" class="btn btn-primary" asp-page="Group AccessDetails" asp-route-usersAccessRights="@Model.UsersAccessRights">@Localizer["Save"]</button>
        }
        <a asp-page="Group Access" asp-route-searchString="@Model.SearchString" asp-route-searchInt="@Model.SearchInt">@Localizer["Back to Group"]</a>
    </div>

cshtml-table UsersAccessRights:

@if (Model.UsersAccessRights != null)
{<table class="table table-striped table-bordered dataTable tableAccessRights" id="tableAccessRights"  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>
                        }
                    }

}


回答1:


Here is a simple demo like below:

1.Model:

public class OutputAccessRights
{
    public int Id { get; set; }
    public bool ChkUserAccessRights { get; set; }
    public string SubMenuDescription { get; set; }
    public string MainMenuDescription { get; set; }
    public bool ChkAddRight { get; set; }
}

2.Edit.cshtml:

<form method="post">
    <table class="table table-striped table-bordered dataTable tableAccessRights" id="tableAccessRights" style="width:100%">
        <thead>
            <tr>
                <th>
                    MainMenu
                </th>
                <th>
                    SubMenu
                </th>
                <th>
                    Operation
                </th>
            </tr>
        </thead>
        <tbody>
            @for (var i = 0; i < Model.UsersAccessRights.Count(); i++)                        
                {

                    <tr>
                        <td>
                            <input class="form-check-inline"asp-for="UsersAccessRights[i].ChkUserAccessRights" />
                            @Model.UsersAccessRights[i].MainMenuDescription
                        </td>
                        <td>
                            @Model.UsersAccessRights[i].SubMenuDescription
                        </td>
                        <td>
                            <input class="form-check-inline" asp-for="UsersAccessRights[i].ChkAddRight" />
                            <label for="chkAddRight">Insert</label>
                        </td>
                    </tr>

                }                
        </tbody>
    </table>
    <button type="submit" class="btn btn-primary" >Save</button>
</form>

3.Edit.cshtml.cs:

public class EditModel : PageModel
{
    private readonly RazorContext _context;

    public EditModel(RazorContext context)
    {
        _context = context;
    }

    [BindProperty]
    public IList<OutputAccessRights> UsersAccessRights { get; set; }

    public async Task<IActionResult> OnGetAsync()
    {
        UsersAccessRights = await _context.OutputAccessRights.ToListAsync();

        if (UsersAccessRights == null)
        {
            return NotFound();
        }
        return Page();
    }

    public async Task<IActionResult> OnPostAsync(IList<OutputAccessRights> usersAccessRights)
    {
        //do your stuff...
    }
}

4.Result:



来源:https://stackoverflow.com/questions/60018401/ilist-returning-count-0-on-posting-back-updated-checkboxes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!