1

我正在尝试将角色添加到我的默认mvc6项目中。我在我的 register.cshtml得到一个例外如何在注册用户实体框架时纠正异常

@model TransparentEnergy.Models.RegisterViewModel 

<div class="col-md-6"> 
    <div class="card"> 
     <div class="card-header"> 
      <h2>Register New User</h2> 
     </div> 
     <div class="card-body card-padding"> 
      @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) 
      { 
       @Html.AntiForgeryToken() 
       @Html.ValidationSummary("", new { @class = "text-danger" }) 
      <div class="card-body card-padding"> 
       <div class="form-group"> 
        <label for="Email" class="col-sm-2 control-label">Email</label> 
        <div class="col-sm-10"> 
         <div class="fg-line"> 
          @Html.TextBoxFor(m => m.Email, new { @class = "form-control fg-input" }) 
         </div> 
        </div> 
       </div> 
       <div class="form-group"> 
        <label for="Password" class="col-sm-2 control-label">Password</label> 
        <div class="col-sm-10"> 

         <div class="fg-line"> 
          @Html.PasswordFor(m => m.Password, new { @class = "form-control fg-input" }) 
         </div> 
        </div> 
       </div> 
       <div class="form-group"> 
        <label for="ConfirmPassword" class="col-sm-2 control-label">Confirm Password</label> 
        <div class="col-sm-10"> 

         <div class="fg-line"> 
          @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control fg-input" }) 
         </div> 
        </div> 
       </div> 
       <div class="form-group"> 
        <label for="Name" class="col-sm-2 control-label">Role Type</label> 
        <div class="col-sm-10"> 
         <div class="fg-line select"> 
           @Html.DropDownList("Name", null, new { @class = "form-control selectpicker"}) 
         </div> 
        </div> 
       </div> 
       <div class="form-group"> 
        <div class="col-sm-offset-2 col-sm-10"> 
         <button type="submit" class="btn btn-primary btn-lg">Register</button> 
        </div> 
       </div> 
      </div> 

     } 

     @section Scripts { 
      @Scripts.Render("~/bundles/jqueryval") 
     } 

    </div> 
</div> 

错误消息

具有关键的“名称”的类型“System.String”的ViewData的项目,但必须是“IEnumerable”类型。

的AccountController

// GET: /Account/Register 
    [AllowAnonymous] 
    public ActionResult Register() 
    { 
     ViewBag.Name = new SelectList(context.Roles.ToList(), "Name", "Name"); 
     return View(); 
    } 

    // 
    // POST: /Account/Register 
    [HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Register(RegisterViewModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; 
      var result = await UserManager.CreateAsync(user, model.Password); 
      if (result.Succeeded) 
      { 

       //Assign Role to user Here 
       await this.UserManager.AddToRoleAsync(user.Id, model.Name); 
       //Ends Here 


       await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); 

       return RedirectToAction("Index", "Home"); 
      } 
      AddErrors(result); 
     } 

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

RegisterViewModel

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

    [Required] 
    [Display(Name = "Full Name")] 
    public string FullName { get; set; } 

    [Required] 
    [Display(Name = "Role")] 
    public string Name { get; set; } 
} 
+2

不能为一个字符串属性创建一个下拉列表它必须是IEnumerable类型的。它*在您插入的例外文本中直接*。你想做什么?你期待什么结果? – Patrick

+0

事情是我遵循一个似乎适用于很多人工智能的例子。我正在尝试实现用户角色。当我注册一个用户时,我想要一个可供选择的角色选择框。选择框是diplaying他们但错误来提交。这里是示例 – texas697

+0

http://www.dotnetcurry.com/aspnet-mvc/1102/aspnet-mvc-role-based-security – texas697

回答

2

您的视图模型:

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

[Required] 
[Display(Name = "Full Name")] 
public string FullName { get; set; } 

[Required] 
[Display(Name = "Role")] 
public string Name { get; set; } 

//store roles 
public IEnumerable<Role> Roles {get;set;} 
} 

您的GET控制器:

[AllowAnonymous] 
public ActionResult Register() 
{ 
    RegisterViewModel model = new RegisterViewModel(); 
    //Controller should not intervene with presentation logic 
    //so SelectList is not used here 
    model.Roles = context.Roles.ToList(); 

    return View(model); 
} 

最后你查看,更改:

@Html.DropDownList("Name", null, new { @class = "form-control selectpicker"}) 

要:

@Html.DropDownListFor(m => m.Name, 
      new SelectList(Model.Roles, "Name", "Name"), 
      new { @class = "form-control selectpicker"}) 

更新: 您的帖子控制器:

model.Roles = context.Roles.ToList(); 
    // If we got this far, something failed, redisplay form 
    return View(model); 
+0

实际上遇到错误...........值不能为空。 参数名称:项目 – texas697

+0

第46行:@ Html.DropDownListFor(m => m.Name, – texas697

+0

这个错误发生在数据验证失败的权利?例如,当电子邮件是空白的,你点击注册按钮。如果是这样的话,您需要在返回视图之前再次在后控制器中填充角色 – PythaLye