public void add(Ledger model, LedgerLeader Leader, LedgerLeader Director) { if (model.Id == 0) { database.Ledger.Add(model); } else { database.Entry(model).State = System.Data.Entity.EntityState.Modified; } database.SaveChanges(); var list = database.LedgerLeader.Where(t => t.LedgerId == model.Id).ToList(); if (list.Count != 0) { database.LedgerLeader.RemoveRange(list); } Leader.LedgerId = model.Id; database.LedgerLeader.Add(Leader); database.SaveChanges(); Director.LedgerId = model.Id; database.LedgerLeader.Add(Director); database.SaveChanges(); }
public override void loadForeignKey(Ledger entity) { if (entity == null) { return; } LedgerLeader leader = database.LedgerLeader.Where(t => t.LedgerId == entity.Id && t.Type == (int)LeaderType.Leader).FirstOrDefault(); LedgerLeader director = database.LedgerLeader.Where(t => t.LedgerId == entity.Id && t.Type == (int)LeaderType.Director).FirstOrDefault(); if (leader == null) { return; } if (director == null) { return; } entity.LeaderStr = leader.Name + "," + leader.Duty + "," + leader.Phone; entity.DirectorStr = director.Name + "," + director.Duty + "," + director.Phone; entity.DateString = entity.PetitionDate.ToString("yyyy-MM-dd"); }
public ActionResult CreatePost(Ledger model) { User user = TheApp.currentUser; if (user == null) { return(Redirect("/Home/Login")); } MyJsonResult result = new MyJsonResult(); if (model == null) { result.message = "请正确填写信息"; return(Json(result)); } if (string.IsNullOrEmpty(model.LeaderStr)) { result.message = "请填写包家领导信息"; return(Json(result)); } if (string.IsNullOrEmpty(model.DirectorStr)) { result.message = "请填写维稳负责人信息"; return(Json(result)); } LedgerLeader leader = JsonConvert.DeserializeObject <LedgerLeader>(model.LeaderStr); LedgerLeader director = JsonConvert.DeserializeObject <LedgerLeader>(model.DirectorStr); if (leader == null || director == null) { throw new ApplicationException("解析失败"); } model.CountyId = user.CountyId; LedgerDao dao = new LedgerDao(); dao.add(model, leader, director); dao.Dispose(); result.success = true; return(Json(result)); }