public ActionResult AssoEdit(short infoId, short riftId, string submit, List <AssoVar> varieties) { if (ModelState.IsValid) { List <byte> currentAssoId = db.VarietyOfInhabitants.Where(x => x.RiftId == riftId).Select(x => x.RaceId).ToList(); if (varieties != null) { foreach (AssoVar variety in varieties) { //if the association already exists if (currentAssoId.Contains(variety.RaceId)) { VarietyOfInhabitant toEdit = db.VarietyOfInhabitants.Where(x => x.RaceId == variety.RaceId && x.RiftId == riftId).First(); toEdit.RaceOrder = variety.RaceOrder; db.Entry(toEdit).State = EntityState.Modified; db.SaveChanges(); currentAssoId.Remove(variety.RaceId); } else { VarietyOfInhabitant toAdd = new VarietyOfInhabitant { RiftId = riftId, RaceId = variety.RaceId, RaceOrder = variety.RaceOrder }; db.VarietyOfInhabitants.Add(toAdd); } } } if (currentAssoId.Count != 0) { foreach (byte id in currentAssoId) { VarietyOfInhabitant gone = db.VarietyOfInhabitants.Where(x => x.RiftId == riftId && x.RaceId == id).FirstOrDefault(); db.VarietyOfInhabitants.Remove(gone); } } db.SaveChanges(); return(Json(true)); } //if model fails ViewBag.Races = db.Races.OrderBy(r => r.RaceName).ToList(); var rift = db.Rifts.Find(riftId); AssoRiftVM model = new AssoRiftVM { InfoId = infoId, RiftId = riftId, Submit = submit, Assos = varieties, Name = rift.Nickname }; return(View(model)); }
public AssoVar(VarietyOfInhabitant variety) { RaceId = variety.RaceId; RaceOrder = variety.RaceOrder; }
public ActionResult AssoCreate(short infoId, short riftId, string submit, List <AssoVar> varieties) { var rift = db.Rifts.Where(i => i.RiftId == riftId).FirstOrDefault(); if (ModelState.IsValid) { var info = db.Infos.Where(i => i.InfoId == infoId).FirstOrDefault(); #region Save or Publish? switch (submit) { case "Save Progress": case "Un-Publish": case "Save and Continue": info.IsPublished = false; rift.IsPublished = false; break; case "Publish": case "Save": info.IsPublished = true; rift.IsPublished = true; break; case "Save and associate": break; default: break; } db.Entry(rift).State = EntityState.Modified; db.Entry(info).State = EntityState.Modified; #endregion List <byte> currentAssoId = db.VarietyOfInhabitants.Where(x => x.RiftId == riftId).Select(x => x.RaceId).ToList(); if (varieties != null) { foreach (AssoVar variety in varieties) { //if the association already exists if (currentAssoId.Contains(variety.RaceId)) { VarietyOfInhabitant toEdit = db.VarietyOfInhabitants.Where(x => x.RaceId == variety.RaceId && x.RiftId == riftId).First(); toEdit.RaceOrder = variety.RaceOrder; db.Entry(toEdit).State = EntityState.Modified; db.SaveChanges(); currentAssoId.Remove(variety.RaceId); } else { VarietyOfInhabitant toAdd = new VarietyOfInhabitant { RiftId = riftId, RaceId = variety.RaceId, RaceOrder = variety.RaceOrder }; db.VarietyOfInhabitants.Add(toAdd); } } } if (currentAssoId.Count != 0) { foreach (byte id in currentAssoId) { VarietyOfInhabitant gone = db.VarietyOfInhabitants.Where(x => x.RiftId == riftId && x.RaceId == id).FirstOrDefault(); db.VarietyOfInhabitants.Remove(gone); } } db.SaveChanges(); return(Json(true)); } //if model fails ViewBag.Races = db.Races.OrderBy(r => r.RaceName).ToList(); AssoRiftVM model = new AssoRiftVM { InfoId = infoId, RiftId = riftId, Submit = submit, Assos = varieties, Name = rift.Nickname }; ViewBag.Selected = varieties.Select(v => v.RaceId).ToList(); return(Json(model)); }