I'm Trying to populate a DropDownList With Roles from my AspNetRoles table, in a Register form. When i hit Create i get this error :
There is no ViewData item of type 'IEnumerable' that has the key 'Name'.
AccountController.cs
- [AllowAnonymous]
- public ActionResult Register()
- {
- ViewBag.Name = new SelectList(context.Roles, "Name", "Name");
- return View();
- }
- //
- // POST: /Account/Register
- [HttpPost]
- [AllowAnonymous]
- [ValidateAntiForgeryToken]
- public async Task
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
- result= await this.UserManager.AddToRoleAsync(user.Id, model.UserRoles);
- //Ends Here
- await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
- // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
- // Send an email with this link
- // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
- // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
- // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking here");
- return RedirectToAction("Index", "Home");
- }
- AddErrors(result);
- }
- // If we got this far, something failed, redisplay form
- return View(model);
- }
Register.cshtml
- @using System.Web.Mvc.Html
- @model YCS.Models.RegisterViewModel
- @{
- ViewBag.Title = "Register";
- }
@ViewBag.Title.
- @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
- {
- @Html.AntiForgeryToken()
Create a new account.
- @Html.ValidationSummary("", new { @class = "text-danger" })
- class="form-group">
- @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
class="col-md-10">- @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
- class="form-group">
- @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
class="col-md-10">- @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
- class="form-group">
- @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
class="col-md-10">- @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
- class="form-group">
- @Html.Label("Select User Type", new { @class = "col-md-2 control-label" })
class="col-md-10">- @*@Html.DropDownList("Name")*@
- @Html.DropDownList("Name", (SelectList)ViewBag.Roles, "Select ...")
- @*@Html.DropDownListFor(m => m.UserRoles,new SelectList(ViewBag.UserRoles,"Value","Text"), new {@class="form-control"})*@
- class="form-group">class="col-md-offset-2 col-md-10">
- "submit" class="btn btn-default" value="Register" />
I've read most articles on the problem and still now solution, Please help.

Ramesh PalanivelPosted Apr 19, 2018, 2:28 AM
Shriya BramdeoPosted Apr 18, 2018, 8:08 AM
Ramesh PalanivelPosted Apr 18, 2018, 7:56 AM
Ramesh PalanivelPosted Apr 18, 2018, 6:09 AM
Shriya BramdeoPosted Apr 18, 2018, 5:55 AM
Ramesh PalanivelPosted Apr 18, 2018, 5:04 AM
Shriya BramdeoPosted Apr 18, 2018, 4:27 AM
Ramesh PalanivelPosted Apr 18, 2018, 4:14 AM