// JObject here is very very important... public ActionResult <string> Post2([FromBody] JObject entityObj) { //声明要插入的对象 ABD_DOUBLE_SEAL_MST postedEntity = entityObj.ToObject <ABD_DOUBLE_SEAL_MST>(); //获取表格中已有的数据 var orderListEntity = myContext.ABD_DOUBLE_SEAL_MST.Where(d => d.ID.ToString() != null); if (!String.IsNullOrEmpty(postedEntity.ID.ToString())) //修改已有数据 { orderListEntity = orderListEntity.Where(d => d.ID.Equals(postedEntity.ID)); } if (orderListEntity.Count() == 0) //如果表中没有任何数据,则插入新数据 { // INSERT myContext.ABD_DOUBLE_SEAL_MST.Add(postedEntity); myContext.SaveChanges(); return(""); } else //如果表中有数据,则插入新数据 { orderListEntity.First().MEDIUM_END_MOVABLE_RING = postedEntity.MEDIUM_END_MOVABLE_RING; orderListEntity.First().MEDIUM_END_STATIC_RING = postedEntity.MEDIUM_END_STATIC_RING; orderListEntity.First().ATMOSPHERIC_END_MOVABLE_RING = postedEntity.ATMOSPHERIC_END_MOVABLE_RING; orderListEntity.First().ATMOSPHERIC_END_STATIC_RING = postedEntity.ATMOSPHERIC_END_STATIC_RING; orderListEntity.First().SPRING = postedEntity.SPRING; orderListEntity.First().METAL_BASE = postedEntity.METAL_BASE; orderListEntity.First().O_TYPE_RING = postedEntity.O_TYPE_RING; myContext.SaveChanges(); return(""); } }
public ActionResult <string> Delete(string ID) { Boolean isSuccess = true; string data = "Delete success"; if (!String.IsNullOrEmpty(ID)) { String[] ID_LIST = ID.Split(","); foreach (string RESULTS_ITEM in ID_LIST) { ABD_DOUBLE_SEAL_MST delMstEntity = new ABD_DOUBLE_SEAL_MST() { ID = Convert.ToInt32(RESULTS_ITEM) }; if (delMstEntity.Equals(null)) { isSuccess = false; data = "No such data."; break; } else { // FIND DB ITEM myContext.ABD_DOUBLE_SEAL_MST.Attach(delMstEntity); // DELETE DB ITEM var delRes = myContext.ABD_DOUBLE_SEAL_MST.Remove(delMstEntity); if (delRes.State == EntityState.Deleted) { // SAVE CHANGES AND DO NOT RETURN myContext.SaveChanges(); } else { isSuccess = false; data = "No such data."; break; } } } } else { isSuccess = false; data = "No such data."; } JsonRes res = new JsonRes(); res.isSuccess = isSuccess; res.data = data; return(JsonConvert.SerializeObject(res)); }