我正在嘗試將角色添加到我的默認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; }
}
不能爲一個字符串屬性創建一個下拉列表它必須是IEnumerable類型的。它*在您插入的例外文本中直接*。你想做什麼?你期待什麼結果? – Patrick
事情是我遵循一個似乎適用於很多人工智能的例子。我正在嘗試實現用戶角色。當我註冊一個用戶時,我想要一個可供選擇的角色選擇框。選擇框是diplaying他們但錯誤來提交。這裏是示例 – texas697
http://www.dotnetcurry.com/aspnet-mvc/1102/aspnet-mvc-role-based-security – texas697