// GET: Products/Create public ActionResult Create() { // To Bind the category drop down in search section ViewBag.Categories = db.Categories.Where(x => x.IsActive == true); var model = new ProductModel2(); return(View(model)); }
public int UpdateProduct(string apiBaseAddress, string linkApi, ProductModel2 pro) { var res = APIHelper.PostDataToAPIReturnDynamic(apiBaseAddress, linkApi, pro); if (res == null) { res = 0; } return(Convert.ToInt32(res)); }
public ActionResult AddProduct(HttpPostedFileBase imgPro, ProductModel2 product) { if (imgPro.ContentLength > 0) { var path = Path.Combine(Server.MapPath("~/Areas/ShoppingPage/Content/Uploads/ImgProduct/"), Path.GetFileName(imgPro.FileName)); imgPro.SaveAs(path); product.image = imgPro.FileName; } IProductService productService = new ProductService(); string linkAPI = "product/addproduct"; productService.AddProduct(baseAddress, linkAPI, product); return(RedirectToAction("Index")); }
public ActionResult UpdateProduct(HttpPostedFileBase imgPro, ProductModel2 product) { if (imgPro != null) { if (imgPro.ContentLength > 0) { var path = Path.Combine(Server.MapPath("~/Areas/ShoppingPage/Content/Uploads/ImgProduct/"), Path.GetFileName(imgPro.FileName)); imgPro.SaveAs(path); product.image = imgPro.FileName; } } else { ProductModel pro = ProductDAO.ProductDetail(baseAddress, $"product/productdetail?id={product.id}"); product.image = pro.Image; } IProductService productService = new ProductService(); string linkAPI = "product/updateproduct"; productService.UpdateProduct(baseAddress, linkAPI, product); return(RedirectToAction("Index")); }
public ActionResult Create(ProductModel2 model) { var imageTypes = new string[] { "image/gif", "image/jpeg", "image/pjpeg", "image/png" }; if (model.ImageUpload == null || model.ImageUpload.ContentLength == 0) { ModelState.AddModelError("ImageUpload", "This field is required"); } else if (!imageTypes.Contains(model.ImageUpload.ContentType)) { ModelState.AddModelError("ImageUpload", "Please choose either a GIF, JPG or PNG image."); } if (ModelState.IsValid) { var product = new Product2(); product.ProductName = model.ProductName; product.Price = model.Price; product.CategoryId = model.CategoryId; using (var binaryReader = new BinaryReader(model.ImageUpload.InputStream)) product.Image = binaryReader.ReadBytes(model.ImageUpload.ContentLength); db.Products2.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); } // If any error return back to the page ViewBag.Categories = db.Categories.Where(x => x.IsActive == true); return(View(model)); }