public ActionResult WorkInfo(WorkInfoViewModel workInfoVM, HttpPostedFileBase Image) { if (ModelState.IsValid) { ApplicationUser user = _dbContext.Users.Find(User.Identity.GetUserId()); user.WorkPlacement = workInfoVM.WorkPlacement; user.IntroduceYourself = workInfoVM.IntroduceYourself; user.Skills.Clear(); if (workInfoVM.Skills != null) { List<Skill> skills = new List<Skill>(); foreach (int skillId in workInfoVM.Skills) { skills.Add(_dbContext.Skills.Find(skillId)); // user.Skills.Add(_dbContext.Skills.Find(skillId)); } user.Skills = skills; } string path = ""; string pic = ""; _dbContext.SaveChanges(); if (Image != null) { string ext = System.IO.Path.GetExtension(Image.FileName); if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif") { //delete old image try { if (user.ImageUrl != SystemInfo.DefaultProfileImage) { path = Server.MapPath(user.ImageUrl); System.IO.File.Delete(path); } } catch (Exception e) { } pic = user.Id+"_" + Guid.NewGuid()+"_" + System.IO.Path.GetFileName(Image.FileName); path = System.IO.Path.Combine(Server.MapPath("~/Content/ProfileImages"), pic); // file is uploaded Image.SaveAs(path); user.ImageUrl = "~/Content/ProfileImages" + "/" + pic; workInfoVM.ImageUrl = "/Content/ProfileImages" + "/" + pic; _dbContext.SaveChanges(); } else { ModelState.Remove("PopupImageUrl"); ModelState.AddModelError("PopupImageUrl", "Chỉ được up ảnh có đuôi .jpg .jpeg .png .gif "); return View(workInfoVM); } } else { //configModel.PopupImageUrl = config.PopupImageUrl; } } //ViewBag.Skills = ViewBag.Skills = new MultiSelectList(_dbContext.Skills.Select(s => new { Id = s.Id, Name = s.Name }), "Id", "Name"); return View(workInfoVM); }
public ActionResult WorkInfo() { WorkInfoViewModel workInfo = new WorkInfoViewModel(); ApplicationUser user = _dbContext.Users.Find(User.Identity.GetUserId()); if (user != null) { workInfo.WorkPlacement = user.WorkPlacement; workInfo.IntroduceYourself = user.IntroduceYourself; workInfo.Skills = user.Skills.Select(s => s.Id).ToArray(); if (user.ImageUrl == null || user.ImageUrl == string.Empty) { workInfo.ImageUrl = VirtualPathUtility.ToAbsolute(SystemInfo.DefaultProfileImage ); } else { workInfo.ImageUrl = VirtualPathUtility.ToAbsolute(user.ImageUrl); } } //ViewBag.Skills = ViewBag.Skills = new MultiSelectList(_dbContext.Skills.Select(s => new { Id = s.Id, Name = s.Name }), "Id", "Name"); return View(workInfo); }