public ActionResult Edit() { EditProfileFormViewModel viewModel; var userProfile = Helper_Classes.UserHelpers.GetUserProfile(); var returnPage = userProfile == null ? "Home" : "Profile"; //var returnPage = "Home"; if (userProfile == null) { viewModel = new EditProfileFormViewModel() { UserProfile = new UserProfile { ApplicationUserId = User.Identity.GetUserId() }, WeightUnits = _context.WeightUnits.ToList(), HeightUnits = _context.HeightUnits.ToList(), Sexes = _context.Sexes.ToList(), ActivityLevels = _context.ActivityLevels.ToList(), RedirectionPage = returnPage } } ; else { var valForHeighInputA = Convert.ToInt32(GetHeightInCm(1)); var valForHeighInputB = Convert.ToInt32(GetHeightInCm(2)); viewModel = new EditProfileFormViewModel() { UserProfile = userProfile, WeightUnits = _context.WeightUnits.ToList(), HeightUnits = _context.HeightUnits.ToList(), Sexes = _context.Sexes.ToList(), ActivityLevels = _context.ActivityLevels.ToList(), HeightInputA = valForHeighInputA, HeightInputB = valForHeighInputB, RedirectionPage = returnPage }; } return(View(viewModel)); }
public ActionResult Create(EditProfileFormViewModel newUserProfile) { var heightUnitId = newUserProfile.UserProfile.HeightUnitId; var heightUnit = _context.HeightUnits .Where(m => m.Id == heightUnitId) .Select(m => m.Name) .SingleOrDefault(); //Return the same page with erro if (!ModelState.IsValid) { IEnumerable <ModelError> errors; errors = ModelState.Values.SelectMany(v => v.Errors); foreach (var VARIABLE in errors) { System.Diagnostics.Debug.WriteLine(VARIABLE.ErrorMessage); } newUserProfile.WeightUnits = _context.WeightUnits.ToList(); newUserProfile.HeightUnits = _context.HeightUnits.ToList(); newUserProfile.Sexes = _context.Sexes.ToList(); newUserProfile.ActivityLevels = _context.ActivityLevels.ToList(); newUserProfile.HeightInputA = Convert.ToInt32(GetHeightInCm(1)); if (heightUnit == HeightUnits.Feetandinches) { newUserProfile.HeightInputB = Convert.ToInt32(GetHeightInCm(2)); } return(View("Edit", newUserProfile)); } double heightInCm = 0.0d; if (heightUnit == HeightUnits.Cm) { heightInCm = newUserProfile.HeightInputA; } else if (heightUnit == HeightUnits.Feetandinches) { heightInCm = Calculators.FeetAndInchesToCM(newUserProfile.HeightInputA, newUserProfile.HeightInputB); } newUserProfile.UserProfile.HeightInCm = heightInCm; if (Helper_Classes.UserHelpers.GetUserProfile() == null) { _context.UserProfiles.Add(newUserProfile.UserProfile); } else { var userProfileId = Helper_Classes.UserHelpers.GetUserProfile().Id; var currentUserProfile = _context.UserProfiles.SingleOrDefault(u => u.Id == userProfileId); _context.Entry(_context.UserProfiles.SingleOrDefault(u => u.Id == userProfileId)).CurrentValues.SetValues(newUserProfile.UserProfile); } _context.SaveChanges(); return(RedirectToAction("Index", newUserProfile.RedirectionPage)); }