public ActionResult DisplayProducts(int?page) { ViewBag.msg = TempData["success"]; var model = _productBuisness.GetAllProducts(); return(View(model.Select(a => ProductViewModel.Convert(a)).ToPagedList(page ?? 1, 5))); }
public ActionResult UploadNewProduct(String Title, String Description, HttpPostedFileBase photo) { HttpPostedFileBase photo2 = Request.Files["photo"]; if (photo != null && photo.ContentLength > 0) { if (photo.ContentLength > maxImageSize) { ViewBag.Error = "Image File size should be less than 5MB."; return(View()); } var supportedTypes = new[] { "jpg", "jpeg", "png" }; var fileExt = System.IO.Path.GetExtension(photo.FileName).Substring(1); if (!supportedTypes.Contains(fileExt)) { ViewBag.Error = "Invalid type. Only the following types (jpg, jpeg, png) are supported."; return(View()); } // check if file name already exists var fileName = Path.GetFileName(photo.FileName); var fileInfo = MakeUnique(Path.Combine(Server.MapPath("~/Images"), fileName)); photo.SaveAs(fileInfo.ToString()); // create model var model = new ProductViewModel() { Description = Description, Title = Title, ImageURL = "/Images/" + fileInfo.Name }; // save model to database var dbUpdateStatus = _productBuisness.AddProduct(ProductViewModel.Convert(model)); if (dbUpdateStatus) { // db update successful - redirect to list view TempData["success"] = "Product Added Successful"; return(RedirectToAction("DisplayProducts")); } else { // delete the image and show error saving to db msg } } ViewBag.Error = "Image Required."; return(View()); }
public ActionResult DisplayProduct(int id) { var model = _productBuisness.GetProduct(id); return(View(ProductViewModel.Convert(model))); }