public ActionResult UploadAvatar(int id, HttpPostedFileBase file) { if (file != null && file.ContentLength > 0) { try { var fileName = Path.GetFileName(file.FileName); string path = Path.Combine(Server.MapPath("~/Content/avatar/"), Path.GetFileName(file.FileName)); UploadImgViewModel model = new UploadImgViewModel { Avatar = fileName.ToString(), ClientId = id }; _repository.UpdateImg(model); file.SaveAs(path); ViewBag.Message = "Your message for success"; } catch (Exception ex) { ViewBag.Message = "ERROR:" + ex.Message.ToString(); } } else { ViewBag.Message = "Please select file"; } return(RedirectToAction("UserProfile", "Account")); }
public bool UpdateImg(UploadImgViewModel model) { try { var client = _dbContext.Clients.FirstOrDefault(x => x.Id == model.ClientId); client.Avatar = model.Avatar; _dbContext.Entry(client).State = EntityState.Modified; _dbContext.SaveChanges(); return(true); } catch (Exception) { return(false); } }