public ActionResult AddProduct(ProductView productView) { if (ModelState.IsValid) { IObjectSet categories = DB.db.QueryByExample(new shop.Models.Category(productView.Category)); var valid = true; if (categories.Count == 0) { valid = false; ModelState.AddModelError("Category", "Category is required."); } if (productView.Image == null) { ModelState.AddModelError("Image", "Image must be"); } else { if (productView.Image.ContentType != "image/jpeg") { valid = false; ModelState.AddModelError("Image", "Image must be i jpeg picture"); } else { var name = Path.GetFileName(productView.Image.FileName); var fileName = Path.GetFileNameWithoutExtension(name); var extension = Path.GetExtension(name); var i = 0; var path = Path.Combine(Server.MapPath("~/Content/products"), fileName + extension); while (System.IO.File.Exists(path)) { ++i; path = Path.Combine(Server.MapPath("~/Content/products"), fileName + i + extension); } productView.Image.SaveAs(path); } } if (productView.Price == null || productView.Price <= 0) { valid = false; ModelState.AddModelError("Price", "Price must be greather by 0"); } var itemToRemove = Request.Form["attribute"]; if (valid) { Category category = (Category)categories.Next(); Product product = new Product(productView.Name); product.attributes = productView.attribute; product.Price = productView.Price; category.addProduct(product); DB.db.Store(category.Products); return RedirectToAction("Products", "Admin"); } } productView.Categories = Category.GetAll(); return View(productView); }
public ActionResult AddProduct() { ProductView productView = new ProductView(); productView.Categories = Category.GetAll(); return View(productView); }