public ActionResult AddUser(FormCollection fc, HttpPostedFileBase Image) { ViewBag.Title = Constant.ADD_USER; AddUserViewModel auvm = AddUserCommon(); var imageName = ""; if (Image != null && Image.ContentLength > 0) { // The user uploaded a file => process it here imageName = FileExtensions.AppendTimeStamp(Path.GetFileName(Image.FileName)); var path = Path.Combine(Server.MapPath("~/Uploads/Images/Users"), imageName); Image.SaveAs(path); } try { auvm.Username = fc["Username"]; auvm.Email = fc["Email"]; auvm.Password = TestEncryption(fc["Password"]); auvm.ConfirmPassword = TestEncryption(fc["ConfirmPassword"]); auvm.UserType = fc["UserTypeList"]; auvm.Image = (imageName == "" || imageName == null) ? Constant.DEFAULT_USER_IMAGE : imageName; auvm.CreatedDate = DateTime.Now; auvm.ModifiedDate = null; auvm.Status = 1; if (ModelState.IsValid) { if (auvm.Password.Equals(auvm.ConfirmPassword)) { _repUser.AddUser(auvm); return(RedirectToAction("UserList")); } } else { ModelState.AddModelError("Password", "The user name or password provided is incorrect."); } } catch (IOException e) { // Extract some information from this exception, and then // throw it to the parent method. if (e.Source != null) { Console.WriteLine("IOException source: {0}", e.Source); throw; } } return(PartialView(auvm)); }
public ActionResult UpdateUser(FormCollection fc, HttpPostedFileBase Image) { ViewBag.Title = Constant.EDIT_USER_PROFILE; var uuvm = new UpdateUserViewModel(); var imageName = ""; if (Image != null && Image.ContentLength > 0) { // The user uploaded a file => process it here imageName = FileExtensions.AppendTimeStamp(Path.GetFileName(Image.FileName)); var path = Path.Combine(Server.MapPath("~/Uploads/Images/Users"), imageName); Image.SaveAs(path); } //Get existing/previous image var previousImage = _repUser.GetUserList().Where(x => x.Username == SessionHelper.Username).SingleOrDefault().Image; try { uuvm.Username = fc["Username"]; uuvm.Password = TestEncryption(fc["Password"]); uuvm.ConfirmPassword = TestEncryption(fc["ConfirmPassword"]); uuvm.Image = (imageName == "" || imageName == null) ? previousImage : imageName; uuvm.Email = fc["Email"]; uuvm.ModifiedDate = DateTime.Now; if (ModelState.IsValid) { if (uuvm.Password.Equals(uuvm.ConfirmPassword)) { _repUser.UpdateUser(uuvm, SessionHelper.UserId); //Unset Previously existing image either if new image is added or previous image is not used if (uuvm.Image != previousImage) { string filePath = Server.MapPath("~/Uploads/Images/Users/" + previousImage); if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } } return(RedirectToAction("UserProfile")); } } else { ModelState.AddModelError("Password", "The user name or password provided is incorrect."); } } catch (IOException e) { // Extract some information from this exception, and then // throw it to the parent method. if (e.Source != null) { Console.WriteLine("IOException source: {0}", e.Source); throw; } } return(View(uuvm)); }