public IActionResult Post(string values) { var model = new m_BPMaster(); var _values = JsonConvert.DeserializeObject <IDictionary>(values); PopulateModel(model, _values); if (!TryValidateModel(model)) { return(BadRequest(GetFullErrorMessage(ModelState))); } ////checked duplicate if (_context.m_BPMaster.Any(o => o.BPCode == model.BPCode)) { ModelState.AddModelError("ModelError", string.Format("BP Code {0} is duplicate key.", model.BPCode)); return(BadRequest(GetFullErrorMessage(ModelState))); } //Add Create date model.CreateDate = DateTime.Now; model.TransDate = DateTime.Now; var result = _context.m_BPMaster.Add(model); _context.SaveChanges(); return(Json(result.Entity.BPCode)); }
private void PopulateModel(m_BPMaster model, IDictionary values) { string BPCODE = nameof(m_BPMaster.BPCode); string BPNAME = nameof(m_BPMaster.BPName); string BPTYPE = nameof(m_BPMaster.BPType); string BPADDRESS1 = nameof(m_BPMaster.BPAddress1); string BPADDRESS2 = nameof(m_BPMaster.BPAddress2); string BPADDRESS3 = nameof(m_BPMaster.BPAddress3); string BPADDRESS4 = nameof(m_BPMaster.BPAddress4); string BPADDRESS5 = nameof(m_BPMaster.BPAddress5); string TRANS_DATE = nameof(m_BPMaster.TransDate); string CREATE_DATE = nameof(m_BPMaster.CreateDate); string MODIFY_BY = nameof(m_BPMaster.ModifyBy); string BPADDRESS6 = nameof(m_BPMaster.BPAddress6); string TAG_FORMAT = nameof(m_BPMaster.TagFormat); string PACKING_ID = nameof(m_BPMaster.PackingID); if (values.Contains(BPCODE)) { model.BPCode = Convert.ToString(values[BPCODE]); } if (values.Contains(BPNAME)) { model.BPName = Convert.ToString(values[BPNAME]); } if (values.Contains(BPTYPE)) { model.BPType = Convert.ToString(values[BPTYPE]); } if (values.Contains(BPADDRESS1)) { model.BPAddress1 = Convert.ToString(values[BPADDRESS1]); } if (values.Contains(BPADDRESS2)) { model.BPAddress2 = Convert.ToString(values[BPADDRESS2]); } if (values.Contains(BPADDRESS3)) { model.BPAddress3 = Convert.ToString(values[BPADDRESS3]); } if (values.Contains(BPADDRESS4)) { model.BPAddress4 = Convert.ToString(values[BPADDRESS4]); } if (values.Contains(BPADDRESS5)) { model.BPAddress5 = Convert.ToString(values[BPADDRESS5]); } 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]); } if (values.Contains(BPADDRESS6)) { model.BPAddress6 = Convert.ToString(values[BPADDRESS6]); } if (values.Contains(TAG_FORMAT)) { model.TagFormat = Convert.ToString(values[TAG_FORMAT]); } if (values.Contains(PACKING_ID)) { model.PackingID = Convert.ToString(values[PACKING_ID]); } }