public async Task <IActionResult> Create([FromForm] MasterAssetCategory MasterAssetCategory) { if (ModelState.IsValid) { bool isCodeExist = _context.MasterAssetCategory.Any(m => m.Code.Equals(MasterAssetCategory.Code)); if (isCodeExist) { ModelState.AddModelError("Code", "Code Already Exists!"); return(View(MasterAssetCategory)); //return RedirectToAction(nameof(Create), new { error = "Code exists" }); } string newJson = JsonConvert.SerializeObject(MasterAssetCategory); MasterAssetCategory.CompanyId = 123; MasterAssetCategory.IsUsed = true; MasterAssetCategory.CreatedBy = _configuration.GetValue <string>("HardcodeValue:Createdby"); MasterAssetCategory.CreatedDatetime = DateTime.UtcNow; _context.Add(MasterAssetCategory); await _context.SaveChangesAsync(); AuditService.InsertActionLog(MasterAssetCategory.CompanyId, MasterAssetCategory.CreatedBy, "Create", "Master Asset Category", null, newJson); return(RedirectToAction(nameof(Index))); } return(View(MasterAssetCategory)); }
public async Task <IActionResult> Edit(int id, [FromForm] MasterAssetCategory MasterAssetCategory) { if (id != MasterAssetCategory.Id) { return(NotFound()); } if (ModelState.IsValid) { try { bool isCodeExist = _context.MasterAssetCategory.Any(m => m.Code.Equals(MasterAssetCategory.Code) && !m.Id.Equals(id)); if (isCodeExist) { ModelState.AddModelError("Code", "Code Already Exists!"); return(View(MasterAssetCategory)); //return RedirectToAction(nameof(Index), new { error = "Code exists" }); } MasterAssetCategory db_Asset_Category_Master = _context.MasterAssetCategory.FirstOrDefault(m => m.Id.Equals(MasterAssetCategory.Id)); OldData oldData = new OldData(); oldData.Code = db_Asset_Category_Master.Code; oldData.Name = db_Asset_Category_Master.Name; oldData.Remark = db_Asset_Category_Master.Remark; oldData.Status = db_Asset_Category_Master.Status; string oldJson = JsonConvert.SerializeObject(oldData); string newJson = JsonConvert.SerializeObject(db_Asset_Category_Master); db_Asset_Category_Master.Code = MasterAssetCategory.Code; db_Asset_Category_Master.Name = MasterAssetCategory.Name; db_Asset_Category_Master.UtilizeL = MasterAssetCategory.UtilizeL; db_Asset_Category_Master.Status = MasterAssetCategory.Status; db_Asset_Category_Master.Remark = MasterAssetCategory.Remark; db_Asset_Category_Master.ModifiedBy = _configuration.GetValue <string>("HardcodeValue:Createdby"); db_Asset_Category_Master.ModifiedDatetime = DateTime.UtcNow; AuditService.InsertActionLog(db_Asset_Category_Master.CompanyId, db_Asset_Category_Master.CreatedBy, "Edit", "Master Asset Category", oldJson, newJson); _context.Update(db_Asset_Category_Master); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!Asset_Category_MasterExists(MasterAssetCategory.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(MasterAssetCategory)); }
private bool UpdateDm(string code, string name, string remark, string utilizeLife, string codeFormat, bool status) { bool res = false; try { MasterAssetCategory asset_Category_Master = _context.MasterAssetCategory.FirstOrDefault(m => m.Code.Equals(code)); OldData oldData = new OldData(); oldData.Code = asset_Category_Master.Code; oldData.Name = asset_Category_Master.Name; oldData.Remark = asset_Category_Master.Remark; oldData.Status = asset_Category_Master.Status; string oldJson = JsonConvert.SerializeObject(oldData); string newJson = JsonConvert.SerializeObject(asset_Category_Master); if (asset_Category_Master != null) { asset_Category_Master.Name = name; asset_Category_Master.Remark = remark; asset_Category_Master.Status = status; asset_Category_Master.UtilizeL = utilizeLife; asset_Category_Master.CodeFormat = codeFormat; asset_Category_Master.ModifiedDatetime = DateTime.UtcNow; asset_Category_Master.ModifiedBy = _configuration.GetValue <string>("HardcodeValue:Modifiedby"); AuditService.InsertActionLog(asset_Category_Master.CompanyId, asset_Category_Master.CreatedBy, "Edit", "Master Asset Category", oldJson, newJson); _context.SaveChanges(); res = true; } } catch (Exception ex) { res = false; } return(res); }
private bool CreateDm(string code, string name, string remark, string utilizeLife, string codeFormat, bool status) { bool res = false; MasterAssetCategory asset_Category_Master = new MasterAssetCategory(); try { NewData newData = new NewData(); newData.Code = code; newData.Name = name; newData.Remark = remark; newData.Status = status; string newJson = JsonConvert.SerializeObject(newData); asset_Category_Master.Code = code; asset_Category_Master.Name = name; asset_Category_Master.Remark = remark; asset_Category_Master.UtilizeL = utilizeLife; asset_Category_Master.CodeFormat = codeFormat; asset_Category_Master.CompanyId = 123; asset_Category_Master.IsUsed = true; asset_Category_Master.Status = status; asset_Category_Master.CreatedBy = _configuration.GetValue <string>("HardcodeValue:Createdby"); asset_Category_Master.CreatedDatetime = DateTime.UtcNow; _context.Add(asset_Category_Master); AuditService.InsertActionLog(asset_Category_Master.CompanyId, asset_Category_Master.CreatedBy, "Create", "Master Asset Category", null, newJson); _context.SaveChanges(); res = true; } catch (Exception ex) { res = false; } return(res); }