public ViewResult Create()
        {
            var newLiterature = new LiteratureEditViewModel
            {
                TechnologyIds = new int[] { },
                Title         = "Title of the book",
                Url           = "#",
                Authors       = "Authors of the book",
                Technologies  = PopulateTechnologiesDropdown()
            };

            return(View("Edit", newLiterature));
        }
        public ActionResult Edit(LiteratureEditViewModel literatureVM)
        {
            if (ModelState.IsValid)
            {
                if (literatureVM == null)
                {
                    return(RedirectToAction(nameof(Index)));
                }

                literatureVM.Technologies = PopulateTechnologiesDropdown();//productVM.Products przybiera to co zwraca dropdown
                int?literatureID = literatureVM.LiteratureID;
                //passing dropdown selected items to the collection
                List <SelectListItem> selectedItems = literatureVM.Technologies.Where(p => literatureVM.TechnologyIds.Contains(int.Parse(p.Value))).ToList();
                Literature            literature;
                //new literature
                if (literatureID == 0)
                {
                    literature = new Literature();
                }
                //edit literature
                else
                {
                    literature = repositoryLit.Literatures.FirstOrDefault(o => o.LiteratureID == literatureID);
                }
                repositoryLit.SaveLiterature(literature, literatureVM);

                //remove all technologies for this literature to avoid duplicated keys
                repositoryLitTech.RemoveLiters(literatureVM.LiteratureID);
                //new tech list for literature
                if (selectedItems != null)
                {
                    //make new technology list for this literature
                    literature.LiteraturesTechnologies = new List <LiteratureTechnology>();
                    foreach (var item in selectedItems)
                    {
                        var literatureAdd = new LiteratureTechnology {
                            LiteratureID = literature.LiteratureID, TechnologyID = int.Parse(item.Value)
                        };
                        repositoryLitTech.NewPair(literatureAdd);
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                literatureVM.Technologies = PopulateTechnologiesDropdown();
                return(View(literatureVM));
            }
        }
Ejemplo n.º 3
0
 public void SaveLiterature(Literature literature, LiteratureEditViewModel literatureVM)
 {
     {
         //repo save
         literature.Title   = literatureVM.Title;
         literature.Authors = literatureVM.Authors;
         if (literatureVM.Url == null)
         {
             literature.Url = "#";
         }
         else
         {
             literature.Url = literatureVM.Url;
         }
     }
     if (literatureVM.LiteratureID == 0)
     {
         _context.Literatures.Add(literature);
     }
     _context.SaveChanges();
 }