public ActionResult Create(CreateProductWithCategoryView prod) { if (ModelState.IsValid) { var cat = prod.Categories; this.administratorSvc.CreateProduct(prod); return(RedirectToAction(nameof(GetAllProduct))); } return(View(prod)); }
public ActionResult Create() { var product = new CreateProductWithCategoryView(); var categories = administratorSvc.GetAllCategories().ToList(); foreach (var item in categories) { SelectListItem tempCategory = new SelectListItem(); tempCategory.Text = item.Name; tempCategory.Value = item.Name; product.Categories.Add(tempCategory); } return(View(product)); }
public void CreateProduct(CreateProductWithCategoryView prod) { Product tempProduct = new Product { Title = prod.Title, Content = prod.Content, Price = prod.Price, Category = prod.Category }; this.db.Products.Add(tempProduct); foreach (var item in prod.SelectedCategories) { var currentCategory = this.db.Categories.FirstOrDefault(c => c.Name == item); tempProduct.Categories.Add(currentCategory); } db.SaveChanges(); }