public async Task <IActionResult> Create([Bind("Id,ProductName,Price,DateCreate,Description,CountPurchase,CategoryId,UserId")] Product product, IList <IFormFile> image) { string[] messageString = { "create", product.ProductName }; IList <string> imageName = null; if (ModelState.IsValid) { if (image != null) { imageName = await SaveImageAsync(image, 400, 400); } await _productService.CreateProductAsync(product); foreach (var item in imageName) { var imageItem = new Entities.Models.Image() { ImageName = item, ProductId = product.Id, }; await _imageService.CreateImage(imageItem); } return(Json(new { controller = "product", message = messageString, isValid = true, html = Helper.RenderRazorViewToString(this, "_ViewAllProduct", await _productService.GetAllProductsAsync()) })); } ViewData["CategoryId"] = new SelectList(await _categoryService.GetAllCategoriesAsync(), "Id", "CategoryName"); ViewData["UserId"] = new SelectList(await _accountService.GetAllUsersAsync(), "Id", "FullName"); return(Json(new { controller = "product", message = messageString, isValid = false, html = Helper.RenderRazorViewToString(this, "Create", product) })); }
public ActionResult Create(ProductViewModel productViewModel, HttpPostedFileBase[] files) { var categoryList = _categoryBusiness.GetListWT(); var subcategoryList = _subCategoryBusiness.GetListWT(); productViewModel.CategoryList = categoryList.Select(x => new SelectListItem { Text = x.CategoryName.ToString(), Value = x.CategoryId.ToString() }).ToList(); productViewModel.AvailabilityList = Commom.GlobalMethods.GlobalMethods.GetAvailabilityList(); if (ModelState.IsValid) { Mapper.CreateMap <ProductViewModel, Product>(); Product product = Mapper.Map <ProductViewModel, Product>(productViewModel); product.TokenKey = GlobalMethods.GetToken(); FileOperations.CreateDirectory(Server.MapPath("~/ProductImage")); FileOperations.CreateDirectory(Server.MapPath("~/ProductImage/Thumbnails/")); bool isSuccess = _productBusiness.AddUpdateDeleteProduct(product, "I"); foreach (HttpPostedFileBase file in files) { var image = new Entities.Models.Image(); string extension = System.IO.Path.GetExtension(file.FileName); string filename = Guid.NewGuid().ToString() + extension; image.Images = filename; image.ProductId = product.ProductID; _imageBusiness.AddUpdateDeleteimage(image, "I"); var filePathThumbnail = Server.MapPath("~/ProductImage/Thumbnails"); file.SaveAs(Server.MapPath("~/ProductImage/" + filename)); var thumbnail = FileOperations.CreateThumbnail(Server.MapPath("~/ProductImage/" + filename), 72, 72); FileOperations.SaveBitmapImageToDirectory(thumbnail, Server.MapPath("~/ProductImage/Thumbnails/" + filename)); } if (isSuccess) { TempData["Success"] = "Product Created Successfully!!"; TempData["isSuccess"] = "true"; return(RedirectToAction("Index")); } else { TempData["Success"] = "Failed to create product!!"; TempData["isSuccess"] = "false"; } } return(View(productViewModel)); }
public ActionResult Edit(ProductViewModel prodViewModel) { string JsonStr = ""; bool isSuccess = true; string message = "Product Updated Successfully!!"; Entities.Models.Image images = new Entities.Models.Image(); if (ModelState.IsValid) { try { var product = _productBusiness.GetListWT(c => c.TokenKey == prodViewModel.TokenKey).FirstOrDefault(); product.ProductName = prodViewModel.ProductName; product.ProductCode = prodViewModel.ProductCode; product.BrandId = prodViewModel.BrandId; product.CategoryId = prodViewModel.CategoryId; product.SubCategoryId = prodViewModel.SubCategoryId; product.DiscountPercent = prodViewModel.DiscountPercent; product.ShortDescription = prodViewModel.ShortDescription; product.Description = prodViewModel.Description; product.Availability = prodViewModel.Availability; _productBusiness.Update(product); _unitOfWork.SaveChanges(); } catch { message = "Failed to update product!!"; isSuccess = false; _unitOfWork.Dispose(); } } else { message = ModelState.Values.SelectMany(m => m.Errors).FirstOrDefault().ErrorMessage; isSuccess = false; } TempData["Success"] = message; TempData["isSuccess"] = isSuccess.ToString(); JsonStr = "{\"message\":\"" + message + "\",\"isSuccess\":\"" + isSuccess + "\"}"; return(Json(JsonStr, JsonRequestBehavior.AllowGet)); }
public ActionResult uploadimage(ProductViewModel productViewModel, HttpPostedFileBase[] files) { string JsonStr = ""; bool isSuccess = true; string message = "Image Added Successfully!!"; try { var product = _productBusiness.GetListWT(c => c.TokenKey == productViewModel.TokenKey).FirstOrDefault(); FileOperations.CreateDirectory(Server.MapPath("~/ProductImage")); FileOperations.CreateDirectory(Server.MapPath("~/ProductImage/Thumbnails/")); foreach (HttpPostedFileBase file in files) { var image = new Entities.Models.Image(); string extension = System.IO.Path.GetExtension(file.FileName); string filename = Guid.NewGuid().ToString() + extension; image.Images = filename; image.ProductId = product.ProductID; _imageBusiness.AddUpdateDeleteimage(image, "I"); var filePathThumbnail = Server.MapPath("~/ProductImage/Thumbnails"); file.SaveAs(Server.MapPath("~/ProductImage/" + filename)); var thumbnail = FileOperations.CreateThumbnail(Server.MapPath("~/ProductImage/" + filename), 72, 72); FileOperations.SaveBitmapImageToDirectory(thumbnail, Server.MapPath("~/ProductImage/Thumbnails/" + filename)); } TempData["Success"] = "Image Uploaded Successfully!!"; TempData["isSuccess"] = "true"; } catch (Exception) { TempData["Success"] = "Failed to upload Image!!"; TempData["isSuccess"] = "false"; message = "Failed to upload Image!!"; isSuccess = false; } JsonStr = "{\"message\":\"" + message + "\",\"isSuccess\":\"" + isSuccess + "\"}"; return(Json(JsonStr, JsonRequestBehavior.AllowGet)); }