public IActionResult Post(string values) { var model = new m_MachineMaster(); var _values = JsonConvert.DeserializeObject <IDictionary>(values); PopulateModel(model, _values); if (!TryValidateModel(model)) { return(BadRequest(GetFullErrorMessage(ModelState))); } //checked duplicate if (_context.m_MachineMaster.Any(o => o.MachineCode == model.MachineCode)) { ModelState.AddModelError("ModelError", string.Format("Machine code {0} is duplicate key.", model.MachineCode)); return(BadRequest(GetFullErrorMessage(ModelState))); } //Add Create date model.CreateDate = DateTime.Now; model.TransDate = DateTime.Now; var result = _context.m_MachineMaster.Add(model); _context.SaveChanges(); return(Json(result.Entity.MachineCode)); }
private void PopulateModel(m_MachineMaster model, IDictionary values) { string MACHINE_CODE = nameof(m_MachineMaster.MachineCode); string MACHINE_NAME = nameof(m_MachineMaster.MachineName); string MACHINE_USER_DEF1 = nameof(m_MachineMaster.MachineUserDef1); string MACHINE_USER_DEF2 = nameof(m_MachineMaster.MachineUserDef2); string TRANS_DATE = nameof(m_MachineMaster.TransDate); string CREATE_DATE = nameof(m_MachineMaster.CreateDate); string MODIFY_BY = nameof(m_MachineMaster.ModifyBy); if (values.Contains(MACHINE_CODE)) { model.MachineCode = Convert.ToString(values[MACHINE_CODE]); } if (values.Contains(MACHINE_NAME)) { model.MachineName = Convert.ToString(values[MACHINE_NAME]); } if (values.Contains(MACHINE_USER_DEF1)) { model.MachineUserDef1 = Convert.ToString(values[MACHINE_USER_DEF1]); } if (values.Contains(MACHINE_USER_DEF2)) { model.MachineUserDef2 = Convert.ToString(values[MACHINE_USER_DEF2]); } if (values.Contains(TRANS_DATE)) { model.TransDate = Convert.ToDateTime(values[TRANS_DATE]); } if (values.Contains(CREATE_DATE)) { model.CreateDate = Convert.ToDateTime(values[CREATE_DATE]); } if (values.Contains(MODIFY_BY)) { model.ModifyBy = Convert.ToString(values[MODIFY_BY]); } }