private bool UpdateDm(string code, string name, string remark, bool status) { bool res = false; try { MasterAssetLocation asset_Location_Master = _context.MasterAssetLocation.FirstOrDefault(m => m.Code.Equals(code)); if (asset_Location_Master != null) { asset_Location_Master.Name = name; asset_Location_Master.Remark = remark; asset_Location_Master.Status = status; asset_Location_Master.ModifiedDatetime = DateTime.UtcNow; asset_Location_Master.ModifiedBy = _configuration.GetValue <string>("HardcodeValue:Modifiedby"); _context.SaveChanges(); res = true; } } catch (Exception ex) { res = false; } return(res); }
private bool CreateDm(string code, string name, string remark, bool status) { bool res = false; MasterAssetLocation asset_Location_Master = new MasterAssetLocation(); try { asset_Location_Master.Code = code; asset_Location_Master.Name = name; asset_Location_Master.Remark = remark; asset_Location_Master.CompanyId = 123; asset_Location_Master.IsUsed = true; asset_Location_Master.Status = status; asset_Location_Master.CreatedBy = _configuration.GetValue <string>("HardcodeValue:Createdby"); asset_Location_Master.CreatedDatetime = DateTime.UtcNow; _context.Add(asset_Location_Master); _context.SaveChanges(); res = true; } catch (Exception ex) { res = false; } return(res); }
public async Task <IActionResult> Create([FromForm] MasterAssetLocation asset_Location_Master) { if (ModelState.IsValid) { bool isCodeExist = _context.MasterAssetLocation.Any(m => m.Code.Equals(asset_Location_Master.Code)); if (isCodeExist) { ModelState.AddModelError("Code", "Code Already Exists!"); return(View(asset_Location_Master)); //return RedirectToAction(nameof(Create), new { error = "Code exists" }); } string newJson = JsonConvert.SerializeObject(asset_Location_Master); bool isCodeExists = IsCodeExists(asset_Location_Master.Code); asset_Location_Master.CompanyId = 123; asset_Location_Master.IsUsed = true; asset_Location_Master.Status = asset_Location_Master.Status; asset_Location_Master.CreatedBy = _configuration.GetValue <string>("HardcodeValue:Createdby"); asset_Location_Master.CreatedDatetime = DateTime.UtcNow; _context.Add(asset_Location_Master); await _context.SaveChangesAsync(); AuditService.InsertActionLog(asset_Location_Master.CompanyId, asset_Location_Master.CreatedBy, "Create", "Master Asset Location", null, newJson); return(RedirectToAction(nameof(Index))); } return(View(asset_Location_Master)); }
public async Task <IActionResult> Edit(int id, [FromForm] MasterAssetLocation asset_Location_Master) { if (ModelState.IsValid) { try { bool isCodeExist = _context.MasterAssetLocation.Any(m => m.Code.Equals(asset_Location_Master.Code) && !m.Id.Equals(id)); if (isCodeExist) { ModelState.AddModelError("Code", "Code Already Exists!"); return(View(asset_Location_Master)); //return RedirectToAction(nameof(Index), new { error = "Code exists" }); } MasterAssetLocation db_asset_Location_Master = _context.MasterAssetLocation.FirstOrDefault(m => m.Id.Equals(asset_Location_Master.Id)); if (asset_Location_Master == null || id != asset_Location_Master.Id) { return(NotFound()); } OldData oldData = new OldData(); oldData.Code = db_asset_Location_Master.Code; oldData.Name = db_asset_Location_Master.Name; oldData.Remark = db_asset_Location_Master.Remark; oldData.Status = db_asset_Location_Master.Status; string oldJson = JsonConvert.SerializeObject(oldData); string newJson = JsonConvert.SerializeObject(asset_Location_Master); db_asset_Location_Master.Code = asset_Location_Master.Code; db_asset_Location_Master.Name = asset_Location_Master.Name; db_asset_Location_Master.Status = asset_Location_Master.Status; db_asset_Location_Master.Remark = asset_Location_Master.Remark; db_asset_Location_Master.ModifiedBy = _configuration.GetValue <string>("HardcodeValue:Createdby"); db_asset_Location_Master.ModifiedDatetime = DateTime.UtcNow; _context.Update(db_asset_Location_Master); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!Asset_Location_MasterExists(asset_Location_Master.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(asset_Location_Master)); }
public async Task <FileResult> Export() { string[] names = typeof(MasterAssetLocation).GetProperties().Select(property => property.Name).ToArray(); string[] columnNames = new string[] { "Code", "Name", "Remark", "Status" }; MasterAssetLocation entities = new MasterAssetLocation(); var customers = await _context.MasterAssetLocation.ToListAsync(); //Build the CSV file data as a Comma separated string. string csv = string.Empty; foreach (string columnName in columnNames) { //Add the Header row for CSV file. csv += columnName + ','; } //remove symbol "," behind csv = csv.Remove(csv.Length - 1, 1); //Add new line. csv += "\r\n"; foreach (var customer in customers) { //Add the Data rows. csv += customer.Code.Replace(",", ";") + ','; csv += customer.Name.Replace(",", ";") + ','; if (customer.Remark != null) { csv += customer.Remark.Replace(",", ";") + ','; } else { customer.Remark = ""; csv += customer.Remark.Replace(",", ";") + ','; } if (customer.Status != false) { string Status = "YES"; csv += Status.Replace(",", ";"); } if (customer.Status != true) { string Status = "NO"; csv += Status.Replace(",", ";"); } //Add new line. csv += "\r\n"; } var file = "AssetLocationMaster.csv"; //Download the CSV file. byte[] bytes = Encoding.ASCII.GetBytes(csv); return(File(bytes, "application/text", file)); }