Guest User

Untitled

a guest
Jul 21st, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. View:
  2.  
  3. <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<RegistrationViewModel>" %>
  4.  
  5. <h2>EStudy Registration</h2>
  6.  
  7. <%
  8. using (Html.BeginForm(new { Action = "Register" }))
  9.  
  10. { %>
  11.  
  12.  
  13. <%= Html.ValidationSummary("Registration Errors:") %>
  14.  
  15. FirstName: <%= Html.TextBox("FirstName")%> <br />
  16. <%= Html.ValidationMessage("FirstName","!") %>
  17. LastName: <%= Html.TextBox("LastName")%> <br />
  18. MiddleName: <%= Html.TextBox("MiddleName")%> <br />
  19.  
  20. UserName: <%= Html.TextBox("UserName")%> <br />
  21. Password: <%= Html.Password("Password")%> <br />
  22. Email: <%= Html.TextBox("Email")%> <br />
  23.  
  24. Select Role: <%= Html.DropDownList("SelectedRoleId", Model.Roles) %>
  25.  
  26. <input type="submit" value="Register" />
  27.  
  28. <% }%>
  29.  
  30.  
  31. Controller:
  32.  
  33. public class RegistrationController : BaseController
  34. {
  35. private IRoleRepository _roleRepository;
  36. private IUserRepository _userRepository;
  37.  
  38. public RegistrationController() {}
  39.  
  40. public RegistrationController(IRoleRepository roleRepository, IUserRepository userRepository)
  41. {
  42. _roleRepository = roleRepository;
  43. _userRepository = userRepository;
  44. }
  45.  
  46. public ActionResult Index()
  47. {
  48. var roles = _roleRepository.GetAll();
  49.  
  50. var viewModel = new RegistrationViewModel()
  51. {
  52. Roles = roles != null ? roles.ToSelectList("RoleName","RoleId") : null
  53. };
  54.  
  55. return View(viewModel);
  56. }
  57.  
  58. public ActionResult Confirm()
  59. {
  60. return View("Index");
  61. }
  62.  
  63.  
  64.  
  65. public ActionResult Register(RegistrationViewModel registrationViewModel)
  66. {
  67.  
  68. // convert from view model to the model using auto mapper!
  69. var newUser = new User()
  70. {
  71. Active = true,
  72. DateCreated = DateTime.Now,
  73. DateModified = DateTime.Now,
  74. FirstName = registrationViewModel.FirstName,
  75. LastName = registrationViewModel.LastName,
  76. MiddleName = registrationViewModel.MiddleName,
  77. UserName = registrationViewModel.UserName,
  78. Password = registrationViewModel.Password,
  79. Email = registrationViewModel.Email,
  80. };
  81.  
  82. var role = _roleRepository.GetByName("Student");
  83. newUser.AddRole(role);
  84.  
  85. // add the errors to the modelstate list
  86. try
  87. {
  88. _userRepository.Save(newUser);
  89. }
  90. catch(BrokenRuleException brokenRuleException)
  91. {
  92. ModelState.Merge(brokenRuleException.BrokenRules.ToModelStateDictionary());
  93. }
  94.  
  95. if (ModelState.IsValid)
  96. return View("Confirm");
  97.  
  98. // add the broken rules
  99. TempData["ModelState"] = ModelState;
  100. return View("Index",registrationViewModel); <--- Error is thrown when the View is bind again!
  101. }
  102. }
  103.  
  104.  
  105. View Model:
  106.  
  107. public class RegistrationViewModel
  108. {
  109. [Required(ErrorMessage = "FirstName is required!")]
  110. public string FirstName { get; set; }
  111. [Required(ErrorMessage = "LastName is required!")]
  112. public string LastName { get; set; }
  113. public string MiddleName { get; set; }
  114.  
  115. [Required(ErrorMessage = "UserName is required!")]
  116. public string UserName { get; set; }
  117.  
  118. [Required(ErrorMessage = "Password is required!")]
  119. public string Password { get; set; }
  120.  
  121.  
  122. [Required]
  123. public string Email { get; set; }
  124.  
  125. public int SelectedRoleId { get; set; }
  126.  
  127. public SelectList Roles { get; set; }
  128.  
  129. }
  130.  
  131. Thanks!
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148. THE ERROR IS POINTING TO THE UserName Field.
  149.  
  150. Source Error:
  151.  
  152.  
  153. Line 25: MiddleName: <%= Html.TextBox("MiddleName")%> <br />
  154. Line 26:
  155. Line 27: UserName: <%= Html.TextBox("UserName")%> <br />
  156. Line 28: Password: <%= Html.Password("Password")%> <br />
  157. Line 29: Email: <%= Html.TextBox("Email")%> <br />
  158.  
  159.  
  160.  
  161.  
  162. [NullReferenceException: Object reference not set to an instance of an object.]
  163. System.Web.Mvc.HtmlHelper.GetModelStateValue(String key, Type destinationType) +60
  164. System.Web.Mvc.Html.InputExtensions.InputHelper(HtmlHelper htmlHelper, InputType inputType, String name, Object value, Boolean useViewData, Boolean isChecked, Boolean setId, Boolean isExplicitValue, IDictionary`2 htmlAttributes) +521
  165. System.Web.Mvc.Html.InputExtensions.TextBox(HtmlHelper htmlHelper, String name, Object value, IDictionary`2 htmlAttributes) +34
  166. System.Web.Mvc.Html.InputExtensions.TextBox(HtmlHelper htmlHelper, String name) +60
  167. ASP.views_registration_index_aspx.__RenderContent2(HtmlTextWriter __w, Control parameterContainer) in c:\Projects\EStudyVS2008\EStudySoltution\EStudy\EStudy.Web.UI\Views\Registration\Index.aspx:27
  168. System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
  169. System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
  170. System.Web.UI.Control.Render(HtmlTextWriter writer) +10
Add Comment
Please, Sign In to add comment