void SaveState(PrzedmiotyGrupyViewModel model) { //create comma delimited list of product ids model.SavedRequested = string.Join(",", model.RequestedPrzedmioty.Select(p => p.id_przedmiotu.ToString()).ToArray()); //Available products = All - Requested var wszystkiePrzedmioty = _repo2.GetAllPrzedmioty.ToList(); var zaznaczonePrzedmioty = model.RequestedPrzedmioty.ToList(); var bezZaznaczonego = wszystkiePrzedmioty.Except(zaznaczonePrzedmioty).ToList(); //var przedmioty = .Except(.AsQueryable()).ToList(); model.AvailablePrzedmioty = bezZaznaczonego; }
void RestoreSavedState(PrzedmiotyGrupyViewModel model) { model.RequestedPrzedmioty = new List<Przedmioty>(); //get the previously stored items if (!string.IsNullOrEmpty(model.SavedRequested)) { int[] prodids = model.SavedRequested.Split(',').Select(s => int.Parse(s)).ToArray(); var prods = _repo2.GetAllPrzedmioty.Where(p => prodids.Contains(p.id_przedmiotu)); model.RequestedPrzedmioty.AddRange(prods); } }
void RemoveProducts(PrzedmiotyGrupyViewModel model) { if (model.RequestedSelected != null) { model.RequestedPrzedmioty.RemoveAll(p => model.RequestedSelected.Contains(p.id_przedmiotu)); model.RequestedSelected = null; } }
void AddProducts(PrzedmiotyGrupyViewModel model) { if (model.AvailableSelected != null) { var prods = _repo2.GetAllPrzedmioty.Where(p => model.AvailableSelected.Contains(p.id_przedmiotu)); model.RequestedPrzedmioty.AddRange(prods); model.AvailableSelected = null; } }
public ActionResult PrzypiszPrzedmioty(PrzedmiotyGrupyViewModel model, string add, string remove, string send, int id) { ViewBag.Current = "Grupy"; // Aktualne zaznaczenie zakladki Profil w Menu //Need to clear model state or it will interfere with the updated model ModelState.Clear(); RestoreSavedState(model); if (!string.IsNullOrEmpty(add)) AddProducts(model); else if (!string.IsNullOrEmpty(remove)) RemoveProducts(model); else if (!string.IsNullOrEmpty(send)) { if (ModelState.IsValid) { var grupa = _repo.GetGroupByID(id); grupa.Przedmioty.Clear(); foreach(Przedmioty przedmiot in model.RequestedPrzedmioty){ grupa.Przedmioty.Add(przedmiot); } _repo.EditGroup(grupa); _repo.Save(); _logger.Info("GrupyController.Edit => SUCCES = Edit PRZYPISZPRZEMDIOTY| HTTP POST"); TempData["message"] = "Zauktalizowano grupę!"; TempData["status"] = "valid"; return RedirectToAction("List"); } //todo: implement SendListToSanta method... } SaveState(model); return View(model); }
public ActionResult PrzypiszPrzedmioty(int id) { ViewBag.Current = "Grupy"; // Aktualne zaznaczenie zakladki Profil w Menu var grupa = _repo.GetGroupByID(id); if (grupa != null) { var przedmiotyPrzypisane = grupa.Przedmioty.ToList(); var wszystkiePrzedmioty = _repo2.GetAllPrzedmioty.ToList(); var mozliweDoWyboru = wszystkiePrzedmioty.Except(przedmiotyPrzypisane).ToList(); PrzedmiotyGrupyViewModel model = new PrzedmiotyGrupyViewModel { AvailablePrzedmioty = mozliweDoWyboru, RequestedPrzedmioty = przedmiotyPrzypisane }; model.SavedRequested = string.Join(",", model.RequestedPrzedmioty.Select(p => p.id_przedmiotu.ToString()).ToArray()); return View(model); } else { TempData["message"] = "Taka grupa nie istnieje!"; TempData["status"] = "invalid"; return View(); } }