public ResponseBase Update(int Id, string name, List <LightPercentDetailModel> items) { var result = new ResponseBase(); try { db = new PMSEntities(); var obj = db.P_LightPercent.FirstOrDefault(x => !x.IsDeleted && x.Id == Id); if (obj != null) { obj.Name = name; } var olds = db.P_LightPercent_De.Where(x => !x.IsDeleted && x.LightPercentId == Id); if (olds != null && olds.Count() > 0) { foreach (var item in olds) { item.IsDeleted = true; } } if (items != null && items.Count > 0) { P_LightPercent_De subItem; foreach (var itemObj in items) { subItem = new P_LightPercent_De(); Parse.CopyObject(itemObj, ref subItem); subItem.IsDeleted = false; subItem.LightPercentId = Id; db.P_LightPercent_De.Add(subItem); } } db.SaveChanges(); result.IsSuccess = true; result.Messages.Add(new Message() { Title = "Thông Báo", msg = "Lưu thành công." }); } catch (Exception ex) { } return(result); }
public ResponseBase InsertOrUpdate(LightPercentModel model) { var result = new ResponseBase(); try { using (var _db = new PMSEntities()) { P_LightPercent lightObj; if (model.Id == 0) { lightObj = new P_LightPercent(); Parse.CopyObject(model, ref lightObj); lightObj.IsDeleted = false; if (model.Childs != null && model.Childs.Count > 0) { lightObj.P_LightPercent_De = new List <P_LightPercent_De>(); P_LightPercent_De subItem; foreach (var itemObj in model.Childs) { subItem = new P_LightPercent_De(); Parse.CopyObject(itemObj, ref subItem); subItem.IsDeleted = false; subItem.P_LightPercent = lightObj; lightObj.P_LightPercent_De.Add(subItem); } } _db.P_LightPercent.Add(lightObj); } else { lightObj = _db.P_LightPercent.FirstOrDefault(x => x.Id == model.Id); if (lightObj != null) { lightObj.Name = model.Name; lightObj.P_LightPercent_De.ToList().ForEach(x => _db.P_LightPercent_De.Remove(x)); lightObj.P_LightPercent_De = new List <P_LightPercent_De>(); if (model.Childs.Count > 0) { P_LightPercent_De subItem; foreach (var itemObj in model.Childs) { subItem = new P_LightPercent_De(); Parse.CopyObject(itemObj, ref subItem); subItem.IsDeleted = false; subItem.P_LightPercent = lightObj; lightObj.P_LightPercent_De.Add(subItem); } } } } _db.SaveChanges(); result.IsSuccess = true; result.Messages.Add(new Message() { Title = "Thông Báo", msg = "Lưu thành công." }); } } catch (Exception ex) { } return(result); }