private void PopulateModel(m_Shift model, IDictionary values) { string SHIFT_ID = nameof(m_Shift.ShiftID); string SHIFT_TYPE = nameof(m_Shift.ShiftType); string SHIFT_DESC = nameof(m_Shift.ShiftDesc); string START_TIME = nameof(m_Shift.StartTime); string END_TIME = nameof(m_Shift.EndTime); string TRANS_DATE = nameof(m_Shift.TransDate); string CREATE_DATE = nameof(m_Shift.CreateDate); string MODIFY_BY = nameof(m_Shift.ModifyBy); if (values.Contains(SHIFT_ID)) { model.ShiftID = Convert.ToInt32(values[SHIFT_ID]); } if (values.Contains(SHIFT_TYPE)) { model.ShiftType = Convert.ToString(values[SHIFT_TYPE]); } if (values.Contains(SHIFT_DESC)) { model.ShiftDesc = Convert.ToString(values[SHIFT_DESC]); } if (values.Contains(START_TIME)) { model.StartTime = Convert.ToDateTime(values[START_TIME]); } if (values.Contains(END_TIME)) { model.EndTime = Convert.ToDateTime(values[END_TIME]); } 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]); } }
public IActionResult Post(string values) { var model = new m_Shift(); var _values = JsonConvert.DeserializeObject <IDictionary>(values); PopulateModel(model, _values); if (!TryValidateModel(model)) { return(BadRequest(GetFullErrorMessage(ModelState))); } //checked duplicate //if (_context.m_Shift.Any(o => o.ShiftID == model.ShiftID)) //{ // ModelState.AddModelError("ModelError", string.Format("Shift Id {0} is duplicate key.", model.ShiftID)); // return BadRequest(GetFullErrorMessage(ModelState)); //} var maxValue = _context.m_Shift.Max(x => x.ShiftID); var maxRow = _context.m_Shift.First(x => x.ShiftID == maxValue); if (maxRow == null) { maxRow.ShiftID = 0; } model.ShiftID = maxRow.ShiftID + 1; //Add Create date model.CreateDate = DateTime.Now; model.TransDate = DateTime.Now; var result = _context.m_Shift.Add(model); _context.SaveChanges(); return(Json(result.Entity.ShiftID)); }