Exemple #1
0
 public ActionResult Edit(ProductCreateEditModel model, HttpPostedFileBase image = null)
 {
     if (ModelState.IsValid)
     {
         if (image != null)
         {
             model.ImageMimeType = image.ContentType;
             model.ImageData     = new byte[image.ContentLength];
             image.InputStream.Read(model.ImageData, 0, image.ContentLength);
         }
         Product product = new Product()
         {
             ProductID     = model.ProductId,
             Name          = model.ProductName,
             Description   = model.Description,
             Price         = model.Price,
             CategoryId    = model.SelectedCategoryId,
             ImageData     = model.ImageData,
             ImageMimeType = model.ImageMimeType
         };
         repository.UpdateProduct(product);
         TempData["message"] = string.Format("{0} has been saved", model.ProductName);
         return(RedirectToAction("Index"));
     }
     else
     {
         //there is something wrong with the data values
         return(View(model));
     }
 }
Exemple #2
0
        public ActionResult Edit(int productID)
        {
            Product product = repository.Products.FirstOrDefault(p => p.ProductID == productID);

            if (product != null)
            {
                ProductCreateEditModel model = new ProductCreateEditModel();
                model.Description        = product.Description;
                model.Price              = product.Price;
                model.ProductName        = product.Name;
                model.ImageData          = product.ImageData;
                model.ImageMimeType      = product.ImageMimeType;
                model.ProductId          = product.ProductID;
                model.SelectedCategoryId = product.CategoryId;
                model.Categories         = _categoryRepository.GetAll().ToList();
                return(View(model));
            }
            return(RedirectToAction("Index"));
        }