Example #1
0
        public ActionResult Create(Product product, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                if (image != null)
                {
                    product.ImageMimeType = image.ContentType;
                    product.ImageData = new byte[image.ContentLength];
                    image.InputStream.Read(product.ImageData, 0, image.ContentLength);
                }
                else
                {
                    byte[] imageByte = System.IO.File.ReadAllBytes(Server.MapPath("~/Images/defaultProduct.gif"));
                    product.ImageData = imageByte;
                    product.ImageMimeType = "image/gif";
                }

                // Date product was created
                product.UserId = WebSecurity.GetUserId(User.Identity.Name);
                product.DataCreated = DateTime.Now;

                db.Products.Add(product);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            return View(product);
        }
Example #2
0
        public ActionResult Edit(Product product, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                Product dbEntry = db.Products.Find(product.ProductId);

                if (image != null)
                {
                    dbEntry.ImageMimeType = image.ContentType;
                    dbEntry.ImageData = new byte[image.ContentLength];
                    image.InputStream.Read(dbEntry.ImageData, 0, image.ContentLength);
                }

                dbEntry.Name = product.Name;
                dbEntry.Description = product.Description;
                dbEntry.Category = product.Category;

                db.SaveChanges();

                return RedirectToAction("index");
            }
            return View(product);
        }