简体   繁体   中英

Post method receive model as empty

I'm trying to add users to specific roles based on this tutorial . But when I tick the checkbox and submit the form, users cannot be added to the role as expected.

It shows that model.Count=0 from the post method.

EditUsersInRole.cshtml

@model List<UserRoleViewModel>
@{
    var roleId = ViewBag.RoleId;
    ViewBag.Title = "Edit Users in Role";
}

<h1>Add or Romove users from this role</h1>
<form asp-controller="Administration" asp-action="EditUsersInRole" method="post">
    <div class="card">
        <div class="card-body">
            @if(Model.Count > 0)
            {
                @foreach(var user in Model)
                {
                    <div class="form-check">
                      <input type="hidden" asp-for="@user.UserId"/>
                      <input type="hidden" asp-for="@user.UserName"/>
                      <input class="form-check-input" asp-for="@user.isSelected" />
                      <label class="form-check-label" asp-for="@user.isSelected">@user.UserName</label>
                    </div>
                }           
            } 

            <input type="submit" value="Update" class="btn btn-primary" />
            <a asp-controller="administration" asp-action="editrole" asp-route-id = "@roleId" class="btn btn-primary">Cancel</a>
        </div>
    </div>
</form>

EditUsersInRole POST method

[HttpPost]
public async Task<IActionResult> EditUsersInRole(List<UserRoleViewModel> model, string Roleid)
{
    if (ModelState.IsValid)
    {
        var role = await roleManager.FindByIdAsync(Roleid);
        if (role == null)
        {
            ViewBag.ErrorMessage = $"RoleId {Roleid} not found";
            return View("NotFound");
        }
        foreach (var item in model)
        {
            var user = await userManager.FindByIdAsync(item.UserId);
            var isInRole = await userManager.IsInRoleAsync(user, role.Name);
            IdentityResult result = null;

            if (item.isSelected && isInRole)
            {
               result = await userManager.AddToRoleAsync(user, role.Name);
            }
            else if (!item.isSelected && isInRole)
            {
                result = await userManager.RemoveFromRoleAsync(user, role.Name);
            }

            //if (!result.Succeeded).....

    return RedirectToAction("EditRole", new {id = Roleid });
}

You are not correctly pass the model as an array to the controller.

For the asp-for tag helper, it should be

asp-for="@Model[i]./*Property*/"
@if (Model.Count > 0)
{
    for (int i = 0; i < Model.Count; i++)
    {
        <div class="form-check">
            <input type="hidden" asp-for="@Model[i].UserId"/>
            <input type="hidden" asp-for="@Model[i].UserName"/>
            <input class="form-check-input" asp-for="@Model[i].isSelected" />
            <label class="form-check-label" asp-for="@Model[i].isSelected">@Model[i].UserName</label>
        </div>
    }           
} 

You have to pass the model properties correctly

@model List<UserRoleViewModel>
@{
    var roleId = ViewBag.RoleId;
    ViewBag.Title = "Edit Users in Role";
}

<h1>Add or Romove users from this role</h1>
<form asp-controller="Administration" asp-action="EditUsersInRole" method="post">
    <div class="card">
        <div class="card-body">
            @if(Model.Count > 0)
            {
                @if (Model.Count > 0)
                {
                    for (int i = 0; i < Model.Count; i++)
                    {
                        <div class="form-check">
                            <input type="hidden" asp-for="@Model[i].UserId"/>
                            <input type="hidden" asp-for="@Model[i].UserName"/>
                            <input class="form-check-input" asp-for="@Model[i].isSelected" />
                            <label class="form-check-label" asp-for="@Model[i].isSelected">@Model[i].UserName</label>
                        </div>
                    }           
                }          
            } 
            <input type="submit" value="Update" class="btn btn-primary" />
            <a asp-controller="administration" asp-action="editrole" asp-route-id = "@roleId" class="btn btn-primary">Cancel</a>
        </div>
    </div>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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