public ActionResult Register(UserRegisterViewModel model) { // Check if the model state is valid based on the data anotations for the register view model if(ModelState.IsValid) { // Use the User Mapper to map from the Registration view model to the domain User object var domainUser = UserMapping.UserFromRegisterViewModel(model); // Try and register the user using the user service var createdUser = _userService.RegisterUser(domainUser); // If the Registration call to the service returns a user the registration was successful if(createdUser != null) { // Correlctly registered, redirect to login page. return RedirectToAction("LogIn"); } else { // The registration failed on the service layer, notify the user tring to register ModelState.AddModelError("","Failed to register your account, please try again later."); return View(model); } } else { // Something is not valid with the View Model, based on the anotations. Dispalys it. return View(model); } }
public static User UserFromRegisterViewModel(UserRegisterViewModel registerViewModel) { var user = new User {Username = registerViewModel.Username, Password = registerViewModel.Password}; return user; }
// // GET : /Account/Register public ActionResult Register() { // Return the register view with an apropriate view model var registerViewModel = new UserRegisterViewModel(); return View(registerViewModel); }