private ProductWithAllCategorysViewModel ConvertFromProductToProductWithAllCategorysViewModel(Product c) { ProductWithCategorysViewModel productWithCategorys = ConvertFromProductToProductWithCategorysViewModel(c); ProductWithAllCategorysViewModel productWithAllCategorys = new ProductWithAllCategorysViewModel(); productWithAllCategorys.Categorys = productWithCategorys.Categorys; productWithAllCategorys.Id = productWithCategorys.Id; productWithAllCategorys.Name = productWithCategorys.Name; productWithAllCategorys.Description = productWithCategorys.Description; productWithAllCategorys.CreatedDate = productWithCategorys.CreatedDate; productWithAllCategorys.UpdatedDate = productWithCategorys.UpdatedDate; productWithAllCategorys.Active = productWithCategorys.Active; productWithAllCategorys.Type = productWithCategorys.Type; productWithAllCategorys.PartnerId = productWithCategorys.PartnerId; productWithAllCategorys.Teacher = productWithCategorys.Teacher; productWithAllCategorys.SeatsCount = productWithCategorys.SeatsCount ?? null; productWithAllCategorys.AssignedUserId = productWithCategorys.AssignedUserId ?? null; productWithAllCategorys.Location = productWithCategorys.Location; productWithAllCategorys.Image = (productWithCategorys.Image == null || productWithCategorys.Image.Length == 0) ? null : productWithCategorys.Image; productWithAllCategorys.ManagerName = productWithCategorys.ManagerName; productWithAllCategorys.PartnerName = productWithCategorys.PartnerName; var categorysList = categoryRepository.Get().Select(ConvertFromCategoryToCategoryViewModel); productWithAllCategorys.AllCategorys = categorysList.ToList(); return productWithAllCategorys; }
public ActionResult EditProductCategorys(ProductWithAllCategorysViewModel product, IEnumerable<int> selectedCategorys) { try { productService.EditProductCategorys(product, (selectedCategorys != null) ? selectedCategorys.ToArray() : null); return RedirectToAction("Details", new { id = product.Id }); } catch (Exception e) { ModelState.AddModelError("", "Unable to save changes"); } product = productService.GetProductWithAllCategorys(product.Id); return View(product); }
/// <summary> /// Редактирование списка категорий продукта /// </summary> /// <param name="product"></param> public void EditProductCategorys(ProductWithAllCategorysViewModel productView, int[] selectedCategorys) { Product product = productRepository.Get(productView.Id); product.UpdatedDate = DateTime.Now; product.Categories.Clear(); SaveChanges(); if (selectedCategorys != null) { foreach (int categoryId in selectedCategorys) { categoryRepository.AddProducts(categoryId, product.Id); } } }