c# - 在内置 Register.cshtml View 中添加 Roles 下拉列表,并在 Reginster 操作方法中检索其选定的值

标签 c# asp.net-mvc visual-studio-2015 asp.net-core asp.net-identity-2

我有一个具有个人用户帐户身份验证的 ASP.NET Core 应用。该应用默认创建 AccountController、RegisterViewModel、Register.cshtml 等,如下所示。我在 RegisterViewModel 中为角色下拉列表添加了两个属性,并在 Register.cshtml View 中显示该下拉列表,以便管理员可以为新创建的用户选择角色。

问题:在以下 Register 的 Post 方法中,如何检索从 Register.cshtml View 中选择的角色管理员?换句话说,在此 Post 操作方法中,如何使用所选角色以便将新创建的用户添加到该角色?

注册ViewModel:

public class RegisterViewModel
{
        [Required]
        [Display(Name = "Login")]
        public string UserName { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }

        [Display(Name = "Select Role")]
        public ApplicationRole SelectedRole { get; set; }
        public IEnumerable<SelectListItem> Roles { get; set; }
}

AccountController操作方法:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.UserName, StateID=model.StateID, Region=model.Region };
                var result = await _userManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await _signInManager.SignInAsync(user, isPersistent: false);
                    _logger.LogInformation(3, "User created a new account with password.");
                    return RedirectToLocal(returnUrl);
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

最佳答案

向您的 View 模型添加选定的参数

[Display(Name = "Selected Role")]
    public int SelectedRole{ get; set; }

然后在您的 View 上设置下拉菜单,如下所示

@Html.DropDownListFor(x => x.SelectedRole, model.Roles)

这会将下拉结果与您的模型联系起来。然后在你的 Controller 模型上。SelectedRole 应该有你选择的 id

关于c# - 在内置 Register.cshtml View 中添加 Roles 下拉列表,并在 Reginster 操作方法中检索其选定的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38961960/

相关文章:

c# - 如何测量 WPF Silverlight FPS 或渲染时间?

c# - 在 ReSharper 7 中,是否可以扩展字符串的语法突出显示?

c# - 在数组中查找文本

C# : Logic Implementation improvement along with performance

jquery - ajax 将数据发布到 MVC Controller 的日期

asp.net-mvc - 使用 EF POCO 类作为 MVC 2 模型(带有数据注释)

c# - AutoMapper 映射函数到成员的获取

c# - 错误: Fatal error encountered during command execution

visual-studio-2015 - Microsoft.vshup.server.httphostx64.exe 已停止工作 - 修补程序失败

c# - 使用 2 个 Visual Studio 2015 实例 : error CS2012 "file is being used by another process"