public ActionResult CreatePost(SoftwareAppliance softwareappliance, int?[] MachinesList) { if (ModelState.IsValid) { db.SoftwareAppliances.Add(softwareappliance); db.SaveChanges(); if (MachinesList != null) { softwareappliance.Machines = new List<Machine>(); foreach (var machine in MachinesList) softwareappliance.Machines.Add(db.Machines.Find(machine)); db.SaveChanges(); } return RedirectToAction("Dashboard", "Home"); } return View("~/Views/Home/Dashboard.cshtml", softwareappliance); }
public ActionResult EditPost(SoftwareAppliance softwareappliance, int?[] MachinesList) { if (ModelState.IsValid) { db.Entry(softwareappliance).State = EntityState.Modified; db.SaveChanges(); //Machines List SoftwareAppliance softwareapplianceFromDB = db.SoftwareAppliances.Include(x => x.Machines).Where(x => x.SoftwareApplianceId == softwareappliance.SoftwareApplianceId).First(); if (softwareapplianceFromDB.Machines == null) softwareapplianceFromDB.Machines = new List<Machine>(); else { foreach (Machine m in db.Machines.ToList()) softwareapplianceFromDB.Machines.Remove(m); db.SaveChanges(); } if (MachinesList != null) { foreach (int machineID in MachinesList) { var machine = db.Machines.Find(machineID); softwareapplianceFromDB.Machines.Add(machine); } db.SaveChanges(); } return RedirectToAction("Dashboard", "Home"); } return View("~/Views/Home/Dashboard.cshtml", softwareappliance); }