ArgumentNullException:值不能为空。 (参数'user')

ArgumentNullException: Value cannot be null. (Parameter 'user')

我的基于 this 教程的代码有问题。 我尝试制作角色列表并从特定角色添加或删除用户。

这是我在控制器中的代码:

[HttpPost]
        public async Task<IActionResult> EditUsersInRole(List<UserRoleViewModel> model, string roleId)
        {
            ViewBag.roleId = roleId;

            var role = await roleManager.FindByIdAsync(roleId);

            if (role == null)
            {
                ViewBag.ErrorMessage = $"Role with ID = {roleId} cannot be found.";
                return View("NotFound");
            }

            for (int i = 0; i < model.Count; i++)
            {
                var user = await userManager.FindByIdAsync(model[i].UserId);

                IdentityResult result = null;

                if (model[i].IsSelected && !(await userManager.IsInRoleAsync(user, role.Name)))
                {
                    result = await userManager.AddToRoleAsync(user, role.Name);
                }
                else if (!model[i].IsSelected && await userManager.IsInRoleAsync(user, role.Name))
                {
                    result = await userManager.RemoveFromRoleAsync(user, role.Name);
                }
                else
                {
                    continue;
                }

                if (result.Succeeded)
                {
                    if (i < (model.Count - 1))
                        continue;
                    else
                        return RedirectToAction("EditRole", new { Id = roleId });
                }
            }

            return RedirectToAction("EditRole", new { Id = roleId });
        }

EditUsersInRole 查看代码:

@model List<collector_forum.ViewModels.UserRoleViewModel>

@{ 
    var roleId = ViewBag.roleId;
}

<form method="post">
    <div class="card">
        <div class="card-header">
            <h2>Add or remove users from this role</h2>
        </div>
        <div class="card-body">
            @for (int i = 0; i < Model.Count; i++)
            {
                <div class="form-check m-1">
                    <input asp-for="@Model[i].IsSelected" class="form-check-input" />
                    <label class="form-check-label" asp-for="@Model[i].IsSelected">
                        @Model[i].UserName
                    </label>
                </div>
            }
        </div>
        <div class="card-footer">
            <input type="submit" value="Update" class="btn btn-primary" style="width: auto;" />
            <a asp-action="EditRole" asp-route-id="@roleId" class="btn btn-primary" style="width: auto;">Cancel</a>
        </div>
    </div>
</form>

和视图模型:

namespace collector_forum.ViewModels
{
    public class UserRoleViewModel
    {
        public string UserId { get; set; }
        public string UserName { get; set; }
        public bool IsSelected { get; set; }
    }
}

然后我得到这个错误:

ArgumentNullException: Value cannot be null. (Parameter 'user')

出现错误Error

我现在很无助

请帮助我。

//编辑。

Html 发帖前查看 - screenshot

Chrome 开发者工具 -> 网络选项卡 before 点击“更新”

然后点击“更新”view

您忘记发送 UserId,您得到的是一个空用户

<input type="hidden" asp-for="@Model[i].UserId"\>

    <div class="card-body">
        @for (int i = 0; i < Model.Count; i++)
        {
            <div class="form-check m-1">
                <input type="hidden" asp-for="@Model[i].UserId" />
                <input asp-for="@Model[i].IsSelected" class="form-check-input" />
                <label class="form-check-label" asp-for="@Model[i].IsSelected">
                    @Model[i].UserName
                </label>
            </div>
        }
    </div>