示例#1
0
        public ActionResult EditProfile(UserProfileViewModel model, HttpPostedFileBase avatar)
        {
            if (!VerificationHelper.CheckPassword(model.OldPassword))
            {
                return(RedirectToAction("Index", "Home"));
            }

            var user = db.Users.Find(model.Id);

            if (model.FirstName != null || model.LastName != null || avatar != null)
            {
                if (user.FirstName != null || user.LastName != null)
                {
                    user.FirstName = model.FirstName;
                }
                user.LastName = model.LastName;


                if (ImageUploadValidator.IsWebFriendlyImage(avatar))
                {
                    var fileName     = Path.GetFileName(avatar.FileName);
                    var justFileName = Path.GetFileNameWithoutExtension(fileName);
                    justFileName = StringUtilities.URLFriendly(justFileName);
                    fileName     = $"{justFileName}_{DateTime.Now.Ticks}{Path.GetExtension(fileName)}";
                    avatar.SaveAs(Path.Combine(Server.MapPath("~/Avatars/"), fileName));
                    user.AvatarPath = "/Avatars/" + fileName;
                }
                db.SaveChanges();
            }

            return(RedirectToAction("Index", "Home"));
        }