public ActionResult Edit(int id, tbl_Parts tbl_Parts, FileHelper FileHelper, PartsViewModels vm) { string actionName = "Edit"; try { _logger.Log(LogLevel.Trace, actionName + " :: started."); using (var db = new WebAppDbContext()) { using (var transaction = db.Database.BeginTransaction()) { try { if (id <= 0) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (!ModelState.IsValid) { _logger.Log(LogLevel.Trace, actionName + " :: ended."); PartsViewModels objPartsViewModels = GetParts(id); return(PartialView("_Edit", objPartsViewModels)); } if (FileHelper.File != null) { string savaPath = ""; string tempPath = ""; tempPath = "~/Images/Parts"; savaPath = Server.MapPath(tempPath); if (!Directory.Exists(savaPath)) { Directory.CreateDirectory(savaPath); } if (tbl_Parts.File_Path != null && tbl_Parts.PICTURE_FILE_NAME != null) { var imgPath = tbl_Parts.File_Path + tbl_Parts.PICTURE_FILE_NAME; if (System.IO.File.Exists(imgPath)) { System.IO.File.Delete(imgPath); } } var fileName = Path.GetFileNameWithoutExtension(FileHelper.File.FileName); var fileExtension = Path.GetExtension(FileHelper.File.FileName); string image = @"\" + fileName + DateTime.Now.Ticks + fileExtension; tbl_Parts.PICTURE_FILE_NAME = image; tbl_Parts.FileExtension = fileExtension; tbl_Parts.File_Path = savaPath; FileHelper.File.SaveAs(savaPath + image); } // Get Current user Id var userId = System.Web.HttpContext.Current.User.Identity.GetUserId(); tbl_Parts.ModifiedByUser = userId; //Get Current Date & Time. tbl_Parts.ModifiedOnDate = DateTime.Now; tbl_Parts.Status = "Active"; db.Entry(tbl_Parts).State = EntityState.Modified; db.SaveChanges(); transaction.Commit(); Alert("Record Updated Sucessfully!!!", NotificationType.success); } catch (Exception ex) { transaction.Rollback(); Exception(ex); Alert("Their is something went wrong!!!", NotificationType.error); } } } } catch (Exception ex) { _logger.Log(LogLevel.Error, actionName + " EXCEPTION :: " + ex.ToString() + " INNER EXCEPTION :: " + ex.InnerException?.ToString()); } _logger.Log(LogLevel.Trace, actionName + " :: ended."); return(RedirectToAction("Index")); }
public ActionResult Create(tbl_Parts tbl_Parts, FileHelper FileHelper) { string actionName = "Create"; try { if (tbl_Parts != null) { _logger.Log(LogLevel.Trace, actionName + " :: started."); using (var db = new WebAppDbContext()) { using (var transaction = db.Database.BeginTransaction()) { try { if (!ModelState.IsValid) { var vm = new PartsViewModels { _tbl_Cage = db.tbl_Cage.ToList(), _tbl_PartType = db.tbl_PartType.ToList(), _tbl_MCAT = db.tbl_MCAT.ToList(), _tbl_Currency = db.tbl_Currency.ToList(), }; return(PartialView("_Create", vm)); } if (FileHelper.File != null) { string savaPath = ""; string tempPath = ""; tempPath = "~/Images/Parts"; savaPath = Server.MapPath(tempPath); if (!Directory.Exists(savaPath)) { Directory.CreateDirectory(savaPath); } var fileName = Path.GetFileNameWithoutExtension(FileHelper.File.FileName); var fileExtension = Path.GetExtension(FileHelper.File.FileName); string image = @"\" + fileName + DateTime.Now.Ticks + fileExtension; tbl_Parts.PICTURE_FILE_NAME = image; tbl_Parts.FileExtension = fileExtension; tbl_Parts.File_Path = savaPath; FileHelper.File.SaveAs(savaPath + image); } var cage = db.tbl_Cage.Where(x => x.CageCode == tbl_Parts.CageCode).FirstOrDefault(); tbl_Parts.CageId = cage.CageId; // Get Current user Id var userId = System.Web.HttpContext.Current.User.Identity.GetUserId(); tbl_Parts.CreatedByUser = userId; tbl_Parts.ModifiedByUser = userId; //Get Current Date & Time. tbl_Parts.CreatedOnDate = DateTime.Now; tbl_Parts.ModifiedOnDate = DateTime.Now; tbl_Parts.Status = "Active"; db.tbl_Parts.Add(tbl_Parts); db.SaveChanges(); transaction.Commit(); Alert("Data Saved Sucessfully!!!", NotificationType.success); } catch (Exception ex) { transaction.Rollback(); Exception(ex); Alert("Their is something went wrong!!!", NotificationType.error); } } } } } catch (Exception ex) { _logger.Log(LogLevel.Error, actionName + " EXCEPTION :: " + ex.ToString() + " INNER EXCEPTION :: " + ex.InnerException?.ToString()); } _logger.Log(LogLevel.Trace, actionName + " :: ended."); return(RedirectToAction("Index")); }