Exemplo n.º 1
0
        //[Route("/UrunEdit")]

        public IActionResult Edit(int id)
        {
            //ProductVM model = _vatancontext.Products.Select(q => new ProductVM()
            //{
            //    ID = q.ID,
            //    ProductName = q.ProductName,
            //    Description = q.Description,
            //    Quantity = q.Quantity,
            //    Price = q.Price,
            //    categories = _vatancontext.Categories.ToList()


            //}).FirstOrDefault(x => x.ID == id);

            //return View(model);



            Product author = _vatancontext.Products.FirstOrDefault(x => x.ID == id);
            List <CategoryCheckVM> categoryChecks = new List <CategoryCheckVM>();

            ProductVM model = new ProductVM();

            model.ProductName = author.ProductName;
            model.Description = author.Description;
            model.Quantity    = author.Quantity;
            model.Price       = author.Price;

            model.categories = _vatancontext.Categories.ToList();
            //model.categoryid = _newscontext.AuthorCategories.Where(q => q.AuthorID == id).Select(q => q.CategoryID).ToArray();
            int[] selectedCategories = _vatancontext.ProductCategories.Where(q => q.Isdeleted == false).Where(q => q.ProductId == id).Select(q => q.CategoryId).ToArray();

            foreach (var item in model.categories)
            {
                CategoryCheckVM categoryCheck = new CategoryCheckVM();
                categoryCheck.categoryid = item.ID;


                foreach (var item2 in selectedCategories)
                {
                    if (item2 == categoryCheck.categoryid)
                    {
                        categoryCheck.IsChecked = true;
                        break;
                    }
                    else
                    {
                        categoryCheck.IsChecked = false;
                    }
                }

                categoryCheck.Name = item.CategoryName;

                categoryChecks.Add(categoryCheck);
            }

            model.categoryCheck = categoryChecks.ToArray();

            return(View(model));
        }
Exemplo n.º 2
0
        AuthorVM GetAuthorForEdit(int id)
        {
            Author author = _newscontext.Authors.FirstOrDefault(x => x.ID == id);
            List <CategoryCheckVM> categoryChecks = new List <CategoryCheckVM>();

            AuthorVM model = new AuthorVM();

            model.Name       = author.Name;
            model.Surname    = author.SurName;
            model.EMail      = author.EMail;
            model.Phone      = author.Phone;
            model.BirthDate  = author.BirthDate;
            model.Categories = _newscontext.Categories.Where(q => q.IsDeleted == false && q.UpperCategoryID == 1).ToList();
            int[] selectedCategories = _newscontext.AuthorCategories.Where(q => q.AuthorID == id).Select(q => q.CategoryID).ToArray();

            foreach (var item in model.Categories)
            {
                CategoryCheckVM categoryCheck = new CategoryCheckVM();
                categoryCheck.categoryid = item.ID;


                foreach (var item2 in selectedCategories)
                {
                    if (item2 == categoryCheck.categoryid)
                    {
                        categoryCheck.IsChecked = true;
                        break;
                    }
                    else
                    {
                        categoryCheck.IsChecked = false;
                    }
                }

                categoryCheck.Name = item.CategoryName;

                categoryChecks.Add(categoryCheck);
            }

            model.categoryCheck = categoryChecks.ToArray();

            return(model);
        }
Exemplo n.º 3
0
        public IActionResult Edit(AuthorVM model, int[] categoryid)
        {
            Author author = _newscontext.Authors.FirstOrDefault(x => x.ID == model.ID);
            List <CategoryCheckVM> categoryChecks = new List <CategoryCheckVM>();

            if (ModelState.IsValid)
            {
                author.Name      = model.Name;
                author.SurName   = model.Surname;
                author.EMail     = model.EMail;
                author.Phone     = model.Phone;
                author.BirthDate = model.BirthDate;

                _newscontext.SaveChanges();

                int authorid = author.ID;

                model.Categories = _newscontext.Categories.Where(q => q.IsDeleted == false).ToList();
                int[] selectedCategories = _newscontext.AuthorCategories.Where(q => q.AuthorID == authorid).Select(q => q.CategoryID).ToArray();

                foreach (var item in model.Categories)
                {
                    CategoryCheckVM categoryCheck = new CategoryCheckVM();

                    categoryCheck.categoryid = item.ID;

                    foreach (var item2 in selectedCategories)
                    {
                        if (item2 == categoryCheck.categoryid)
                        {
                            categoryCheck.IsChecked = true;
                            break;
                        }
                        else
                        {
                            categoryCheck.IsChecked = false;
                        }
                    }

                    categoryCheck.Name = item.CategoryName;
                    categoryChecks.Add(categoryCheck);
                }

                model.categoryCheck = categoryChecks.ToArray();



                foreach (var item in categoryid)
                {
                    if (!selectedCategories.Contains(item))
                    {
                        AuthorCategory authorCategory = new AuthorCategory();
                        authorCategory.CategoryID = item;
                        authorCategory.AuthorID   = authorid;
                        _newscontext.AuthorCategories.Add(authorCategory);
                    }
                }
                _newscontext.SaveChanges();
            }

            else
            {
                return(View(GetAuthorForEdit(model.ID)));
            }

            return(Redirect("/Admin/Author/Index/"));
        }