public ActionResult EditAvatar(EditAvatarViewModel avatar) { var userId = User.Identity.GetUserId(); var user = db.Users.Find(userId); avatar.User = user; avatar.PostsCount = db.Posts.ToList().FindAll(x => x.UserID == userId).Count(); avatar.TopicsCount = db.Topics.ToList().FindAll(x => x.UserID == userId).Count(); if (ModelState.IsValid) { var fileName = Path.GetFileName(avatar.File.FileName); var path = HttpContext.Server.MapPath("~/Content/Avatars/") + avatar.File.FileName; if (user.AvatarFilename != "~/Content/Avatars/default.jpg") { System.IO.File.Delete(HttpContext.Server.MapPath(user.AvatarFilename)); } user.AvatarFilename = "~/Content/Avatars/" + avatar.File.FileName; db.SaveChanges(); avatar.File.SaveAs(path); return(View(avatar)); } else { return(View(avatar)); } }
public ActionResult EditAvatar() { var userId = User.Identity.GetUserId(); var model = new EditAvatarViewModel { User = db.Users.Find(userId), PostsCount = db.Posts.ToList().FindAll(x => x.UserID == userId).Count(), TopicsCount = db.Topics.ToList().FindAll(x => x.UserID == userId).Count(), }; var lastRole = model.User.Roles.Last(); model.UserRole = db.Roles.Find(lastRole.RoleId).Name; return(View(model)); }