public IActionResult Post(string values) { var model = new m_Package(); var _values = JsonConvert.DeserializeObject <IDictionary>(values); PopulateModel(model, _values); if (!TryValidateModel(model)) { return(BadRequest(GetFullErrorMessage(ModelState))); } ////checked duplicate if (_context.m_Package.Any(o => o.PackID == model.PackID)) { ModelState.AddModelError("ModelError", string.Format("Pack ID {0} is duplicate key.", model.PackID)); return(BadRequest(GetFullErrorMessage(ModelState))); } //Add Create date model.CreateDate = DateTime.Now; model.TransDate = DateTime.Now; var result = _context.m_Package.Add(model); _context.SaveChanges(); return(Json(result.Entity.PackID)); }
private void PopulateModel(m_Package model, IDictionary values) { string PACK_ID = nameof(m_Package.PackID); string PACK_DESC = nameof(m_Package.PackDesc); string PACK_TYPE = nameof(m_Package.PackType); string PACK_QTY = nameof(m_Package.PackQty); string TRANS_DATE = nameof(m_Package.TransDate); string CREATE_DATE = nameof(m_Package.CreateDate); string MODIFY_BY = nameof(m_Package.ModifyBy); if (values.Contains(PACK_ID)) { model.PackID = Convert.ToString(values[PACK_ID]); } if (values.Contains(PACK_DESC)) { model.PackDesc = Convert.ToString(values[PACK_DESC]); } if (values.Contains(PACK_TYPE)) { model.PackType = Convert.ToString(values[PACK_TYPE]); } if (values.Contains(PACK_QTY)) { model.PackQty = Convert.ToInt32(values[PACK_QTY]); } 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]); } }