Shriya Bramdeo

Shriya Bramdeo

  • NA
  • 37
  • 3.5k

Populating List with User Roles

Apr 18 2018 3:11 AM
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<SelectListItem>' that has the key 'Name'.
 
AccountController.cs
  1. [AllowAnonymous]  
  2. public ActionResult Register()  
  3. {  
  4. ViewBag.Name = new SelectList(context.Roles, "Name""Name");  
  5. return View();  
  6. }  
  7. //  
  8. // POST: /Account/Register  
  9. [HttpPost]  
  10. [AllowAnonymous]  
  11. [ValidateAntiForgeryToken]  
  12. public async Task<ActionResult> Register(RegisterViewModel model)  
  13. {  
  14. if (ModelState.IsValid)  
  15. {  
  16. var user = new ApplicationUser { UserName = model.Email, Email = model.Email };  
  17. var result = await UserManager.CreateAsync(user, model.Password);  
  18. if (result.Succeeded)  
  19. {  
  20. //Assign Role to user Here  
  21. result= await this.UserManager.AddToRoleAsync(user.Id, model.UserRoles);  
  22. //Ends Here  
  23. await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);  
  24. // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771  
  25. // Send an email with this link  
  26. // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);  
  27. // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);  
  28. // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");  
  29. return RedirectToAction("Index""Home");  
  30. }  
  31. AddErrors(result);  
  32. }  
  33. // If we got this far, something failed, redisplay form  
  34. return View(model);  
  35. }  
Register.cshtml
  1. @using System.Web.Mvc.Html  
  2. @model YCS.Models.RegisterViewModel  
  3. @{  
  4. ViewBag.Title = "Register";  
  5. }  
  6. <h2>@ViewBag.Title.</h2>  
  7. @using (Html.BeginForm("Register""Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))  
  8. {  
  9. @Html.AntiForgeryToken()  
  10. <h4>Create a new account.</h4>  
  11. <hr />  
  12. @Html.ValidationSummary(""new { @class = "text-danger" })  
  13. <div class="form-group">  
  14. @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })  
  15. <div class="col-md-10">  
  16. @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })  
  17. </div>  
  18. </div>  
  19. <div class="form-group">  
  20. @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })  
  21. <div class="col-md-10">  
  22. @Html.PasswordFor(m => m.Password, new { @class = "form-control" })  
  23. </div>  
  24. </div>  
  25. <div class="form-group">  
  26. @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })  
  27. <div class="col-md-10">  
  28. @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })  
  29. </div>  
  30. </div>  
  31. <!--Select Role Type for User-->  
  32. <div class="form-group">  
  33. @Html.Label("Select User Type"new { @class = "col-md-2 control-label" })  
  34. <div class="col-md-10">  
  35. @*@Html.DropDownList("Name")*@  
  36. @Html.DropDownList("Name", (SelectList)ViewBag.Roles, "Select ...")  
  37. @*@Html.DropDownListFor(m => m.UserRoles,new SelectList(ViewBag.UserRoles,"Value","Text"), new {@class="form-control"})*@  
  38. </div>  
  39. </div>  
  40. <div class="form-group">  
  41. <div class="col-md-offset-2 col-md-10">  
  42. <input type="submit" class="btn btn-default" value="Register" />  
  43. </div>  
  44. </div>  
I've read most articles on the problem and still now solution, Please help.

Answers (8)