public IActionResult Post(string values) { var model = new m_BOM(); var _values = JsonConvert.DeserializeObject<IDictionary>(values); PopulateModel(model, _values); var ResourceCheck = _context.m_BOM.Where(b => b.ItemCode == model.ItemCode).Count(); if (ResourceCheck > 0) { return BadRequest("Part No duplicate please check data."); } if (model.Material1 == model.Material2) { return BadRequest("Material#1 duplicate Material#2 please check data."); } if (!TryValidateModel(model)) return BadRequest(GetFullErrorMessage(ModelState)); var result = _context.m_BOM.Add(model); _context.SaveChanges(); return Json(result.Entity.ItemCode); }
private void PopulateModel(m_BOM model, IDictionary values) { string PART_NO = nameof(m_BOM.ItemCode); string MATERIAL1 = nameof(m_BOM.Material1); string MATERIAL2 = nameof(m_BOM.Material2); string BOMUSER_DEF1 = nameof(m_BOM.BOMUserDef1); string BOMUSER_DEF2 = nameof(m_BOM.BOMUserDef2); string BOMUSER_DEF3 = nameof(m_BOM.BOMUserDef3); string BOMUSER_DEF4 = nameof(m_BOM.BOMUserDef4); string TRANS_DATE = nameof(m_BOM.TransDate); string CREATE_DATE = nameof(m_BOM.CreateDate); string MODIFY_BY = nameof(m_BOM.ModifyBy); if (values.Contains(PART_NO)) { model.ItemCode = Convert.ToString(values[PART_NO]); } if (values.Contains(MATERIAL1)) { model.Material1 = Convert.ToString(values[MATERIAL1]); } if (values.Contains(MATERIAL2)) { model.Material2 = Convert.ToString(values[MATERIAL2]); } if (values.Contains(BOMUSER_DEF1)) { model.BOMUserDef1 = Convert.ToString(values[BOMUSER_DEF1]); } if (values.Contains(BOMUSER_DEF2)) { model.BOMUserDef2 = Convert.ToString(values[BOMUSER_DEF2]); } if (values.Contains(BOMUSER_DEF3)) { model.BOMUserDef3 = Convert.ToString(values[BOMUSER_DEF3]); } if (values.Contains(BOMUSER_DEF4)) { model.BOMUserDef4 = Convert.ToString(values[BOMUSER_DEF4]); } 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]); } }