简体   繁体   English

ArgumentNullException:值不能为 null。 (参数“用户”)

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

I have problem with my code based on this tutorial.根据教程,我的代码有问题。 I tried to make list of roles and add or delete user from specific role.我试图列出角色并从特定角色中添加或删除用户。

This is my code in controller:这是我在 controller 中的代码:

[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 View code: 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>

And the ViewModel:和视图模型:

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

Then I got this error:然后我得到了这个错误:

ArgumentNullException: Value cannot be null. ArgumentNullException:值不能为 null。 (Parameter 'user') (参数“用户”)

Error appears Error出现错误错误

I am helpless right now我现在很无助

Please help me.请帮我。

//Edit. //编辑。

Html view before posting - screenshot Html 发布前查看 -截图

Chrome Developer Tools -> Network tab before click on "Update" Chrome 开发人员工具 -> 网络选项卡,然后单击“更新”

And after hit "Update" view在点击“更新”视图

You forgot to send the UserId and you are getting a null user您忘记发送 UserId 并获得 null 用户

<input type="hidden" asp-for="@Model[i].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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ArgumentNullException:值不能为 null。 (参数“项目”) - ArgumentNullException: Value cannot be null. (Parameter 'items') System.ArgumentNullException:&#39;值不能为null。 参数名称:键 - System.ArgumentNullException: 'Value cannot be null. Parameter name: key' ArgumentNullException:值不能为空。 参数名称:connectionString - ArgumentNullException: Value cannot be null. Parameter name: connectionString ArgumentNullException:值不能为null。 参数名称:实体 - ArgumentNullException: Value cannot be null. Parameter name: entity System.ArgumentNullException:值不能为 null。(参数“connectionString”) - System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString') Winform: System.ArgumentNullException: &#39;值不能为空。 (参数&#39;流&#39;)&#39; - Winform: System.ArgumentNullException: 'Value cannot be null. (Parameter 'stream')' System.ArgumentNullException: &#39;值不能为空。 参数名称:items&#39; - System.ArgumentNullException: 'Value cannot be null. Parameter name: items' System.ArgumentNullException: '值不能是 null。 参数名称:providerInvariantName' - System.ArgumentNullException: 'Value cannot be null. Parameter name: providerInvariantName' ArgumentNullException:值不能为null。 参数名称:包含语句上的键 - ArgumentNullException: Value cannot be null. Parameter name: key on Include Statement ArgumentNullException:值不能为null。参数名称:构造函数 - ArgumentNullException: Value cannot be null. Parameter name: constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM