Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- View:
- <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<RegistrationViewModel>" %>
- <h2>EStudy Registration</h2>
- <%
- using (Html.BeginForm(new { Action = "Register" }))
- { %>
- <%= Html.ValidationSummary("Registration Errors:") %>
- FirstName: <%= Html.TextBox("FirstName")%> <br />
- <%= Html.ValidationMessage("FirstName","!") %>
- LastName: <%= Html.TextBox("LastName")%> <br />
- MiddleName: <%= Html.TextBox("MiddleName")%> <br />
- UserName: <%= Html.TextBox("UserName")%> <br />
- Password: <%= Html.Password("Password")%> <br />
- Email: <%= Html.TextBox("Email")%> <br />
- Select Role: <%= Html.DropDownList("SelectedRoleId", Model.Roles) %>
- <input type="submit" value="Register" />
- <% }%>
- Controller:
- public class RegistrationController : BaseController
- {
- private IRoleRepository _roleRepository;
- private IUserRepository _userRepository;
- public RegistrationController() {}
- public RegistrationController(IRoleRepository roleRepository, IUserRepository userRepository)
- {
- _roleRepository = roleRepository;
- _userRepository = userRepository;
- }
- public ActionResult Index()
- {
- var roles = _roleRepository.GetAll();
- var viewModel = new RegistrationViewModel()
- {
- Roles = roles != null ? roles.ToSelectList("RoleName","RoleId") : null
- };
- return View(viewModel);
- }
- public ActionResult Confirm()
- {
- return View("Index");
- }
- public ActionResult Register(RegistrationViewModel registrationViewModel)
- {
- // convert from view model to the model using auto mapper!
- var newUser = new User()
- {
- Active = true,
- DateCreated = DateTime.Now,
- DateModified = DateTime.Now,
- FirstName = registrationViewModel.FirstName,
- LastName = registrationViewModel.LastName,
- MiddleName = registrationViewModel.MiddleName,
- UserName = registrationViewModel.UserName,
- Password = registrationViewModel.Password,
- Email = registrationViewModel.Email,
- };
- var role = _roleRepository.GetByName("Student");
- newUser.AddRole(role);
- // add the errors to the modelstate list
- try
- {
- _userRepository.Save(newUser);
- }
- catch(BrokenRuleException brokenRuleException)
- {
- ModelState.Merge(brokenRuleException.BrokenRules.ToModelStateDictionary());
- }
- if (ModelState.IsValid)
- return View("Confirm");
- // add the broken rules
- TempData["ModelState"] = ModelState;
- return View("Index",registrationViewModel); <--- Error is thrown when the View is bind again!
- }
- }
- View Model:
- public class RegistrationViewModel
- {
- [Required(ErrorMessage = "FirstName is required!")]
- public string FirstName { get; set; }
- [Required(ErrorMessage = "LastName is required!")]
- public string LastName { get; set; }
- public string MiddleName { get; set; }
- [Required(ErrorMessage = "UserName is required!")]
- public string UserName { get; set; }
- [Required(ErrorMessage = "Password is required!")]
- public string Password { get; set; }
- [Required]
- public string Email { get; set; }
- public int SelectedRoleId { get; set; }
- public SelectList Roles { get; set; }
- }
- Thanks!
- THE ERROR IS POINTING TO THE UserName Field.
- Source Error:
- Line 25: MiddleName: <%= Html.TextBox("MiddleName")%> <br />
- Line 26:
- Line 27: UserName: <%= Html.TextBox("UserName")%> <br />
- Line 28: Password: <%= Html.Password("Password")%> <br />
- Line 29: Email: <%= Html.TextBox("Email")%> <br />
- [NullReferenceException: Object reference not set to an instance of an object.]
- System.Web.Mvc.HtmlHelper.GetModelStateValue(String key, Type destinationType) +60
- 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
- System.Web.Mvc.Html.InputExtensions.TextBox(HtmlHelper htmlHelper, String name, Object value, IDictionary`2 htmlAttributes) +34
- System.Web.Mvc.Html.InputExtensions.TextBox(HtmlHelper htmlHelper, String name) +60
- ASP.views_registration_index_aspx.__RenderContent2(HtmlTextWriter __w, Control parameterContainer) in c:\Projects\EStudyVS2008\EStudySoltution\EStudy\EStudy.Web.UI\Views\Registration\Index.aspx:27
- System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
- System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
- System.Web.UI.Control.Render(HtmlTextWriter writer) +10
Add Comment
Please, Sign In to add comment