Ejemplo n.º 1
0
        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));
        }