Esempio n. 1
0
        public ActionResult UserProfile(string submit, ProfileView profile)
        {
            if (submit == "Cancel")
                return RedirectToAction("Index", "Home");

            // Make sure UserName is unique!  If we can find it in the db, it is not
            ApplicationDbContext db = new ApplicationDbContext();

            // Get current user rec
            var user = db.Users.Find(profile.Id);
            bool unique = false;

            if (ModelState.IsValid && profile.UserName != null)
            {
                // Trim whitespace first
                profile.UserName = profile.UserName.Trim();

                // ModelState is valid, so see if UserName was changed
                // Need to see if user changed UserName: compare profile.UserName with user.UserName,
                //  insensitive compare
                bool same = profile.UserName.Equals(user.UserName, StringComparison.Ordinal);
                if (!same)
                {
                    // User changed the name, so let's make sure it's unique
                    var inDb = db.Users.Where(u => u.UserName.Equals(profile.UserName, StringComparison.Ordinal));
                    if (inDb.Count() == 0)
                        unique = true;
                }
                else unique = true;

                if (unique)
                {
                    // New name is unique, go ahead and update record...
                    // Values are good, need to take care with nullable items
                    user.FirstName = profile.FirstName == null ? null : profile.FirstName.Trim();
                    user.LastName = profile.LastName == null ? null : profile.LastName.Trim();
                    user.DisplayName = profile.DisplayName == null ? null : profile.DisplayName.Trim();
                    user.UserName = profile.UserName;
                    user.Email = profile.Email == null ? null : profile.Email.Trim();
                    user.PhoneNumber = profile.Phone == null ? null : profile.Phone.Trim();
                    user.TextMsgNumber = profile.TextMsgNumber == null 
                        ? null : profile.SameAsPhone == null ? profile.TextMsgNumber.Trim() : user.PhoneNumber;
                    user.EmailNotification = profile.NotifyByEmail ?? false;
                    user.TextNotification = profile.NotifyByText ?? false;
                    db.Entry(user).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index", "Home");
                }
            }

            // Something about the data is incorrect, so return for editing
            // Do this in every action prior to view...
            ViewBag.UserModel = ProjectsHelper.LoadUserModel();
            ViewBag.Msg = !unique ? "UserName must be unique, please try a different name"
                : "Validation error -- please review the fields";

            // Need to reset the fields that could be null
            profile.NotifyByText = profile.NotifyByText ?? false;
            profile.NotifyByEmail = profile.NotifyByEmail ?? false;
            return View(profile);

        } 
Esempio n. 2
0
        //
        // GET: /Manage/Profile
        public ActionResult UserProfile()
        {
            ApplicationDbContext db = new ApplicationDbContext();
            var user = db.Users.Find(User.Identity.GetUserId());
            if (user != null)
            {
                ProfileView profile = new ProfileView();
                profile.Id = user.Id;
                profile.FirstName = user.FirstName;
                profile.LastName = user.LastName;
                profile.DisplayName = user.DisplayName;
                profile.UserName = user.UserName;
                profile.Email = user.Email;
                profile.Phone = user.PhoneNumber;
                profile.NotifyByEmail = user.EmailNotification;
                profile.NotifyByText = user.TextNotification;
                profile.TextMsgNumber = user.TextMsgNumber;

                // Do this in every action prior to view...
                ViewBag.UserModel = ProjectsHelper.LoadUserModel();
                return View(profile);
            }
            // Do this in every GET action...
            ViewBag.UserModel = ProjectsHelper.LoadUserModel();
            return View(user);
        }