public ActionResult SaveRegisterDetails(Register registerDetails) { //We check if the model state is valid or not. We have used DataAnnotation attributes. //If any form value fails the DataAnnotation validation the model state becomes invalid. if (ModelState.IsValid) { //create database context using Entity framework using (var databaseContext = new LoginRegistrationInMVCEntities1()) { //If the model state is valid i.e. the form values passed the validation then we are storing the User's details in DB. RegisterUser reglog = new RegisterUser(); //Save all details in RegitserUser object reglog.FirstName = registerDetails.FirstName; reglog.LastName = registerDetails.LastName; reglog.Email = registerDetails.Email; reglog.Password = registerDetails.Password; //Calling the SaveDetails method which saves the details. databaseContext.RegisterUsers.Add(reglog); databaseContext.SaveChanges(); } ViewBag.Message = "User Details Saved"; return(View("Register")); } else { //If the validation fails, we are returning the model object with errors to the view, which will display the error messages. return(View("Register", registerDetails)); } }
//function to check if User is valid or not public RegisterUser IsValidUser(LoginViewModel model) { using (var dataContext = new LoginRegistrationInMVCEntities1()) { //Retireving the user details from DB based on username and password enetered by user. RegisterUser user = dataContext.RegisterUsers.Where(query => query.Email.Equals(model.Email) && query.Password.Equals(model.Password)).SingleOrDefault(); //If user is present, then true is returned. if (user == null) { return(null); } //If user is not present false is returned. else { return(user); } } }