'IList returning count=0 on posting back updated checkboxes

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>
                        }
                    }

}



Solution 1:[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: enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rena