コード例 #1
0
        public ActionResult Create(ProductsViewModel model)
        {
            //傳進去ProductsViewModel model, 回傳
            var validImageTpes = new string[]
            {
                "image/gif",
                "image/jpeg",
                "image/pjpeg",
                "image/png"
            };

            if ( model.ImageUpload != null && !validImageTpes.Contains(model.ImageUpload.ContentType))
            {
                ModelState.AddModelError("ImageUpload", "請選擇 GIF, JPG or PNG 圖片檔案格式。");
            }

            if (ModelState.IsValid)
            {
                var product = new Products
                {
                    Name = model.Name,
                    Description = model.Description,
                    Category_Id = model.Category_Id,
                    Price = model.Price,
                    Author = model.Author,
                    Publisher = model.Publisher,
                    PublishDT = Convert.ToDateTime(model.PublishDT),
                    IsPublic = model.IsPublic
                };

                if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0)
                {
                    ImageDeal imageDeal = new ImageDeal();
                    string imageUrl = imageDeal.Upload(model.ImageUpload);
                    product.PhotoUrl = imageUrl;

                }

                db.Products.Add(product);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.Category_Id = new SelectList(db.Categories, "Id", "Name", model.Category_Id);
            return View(model);
        }
コード例 #2
0
        public ActionResult Edit(int? id, ProductsViewModel model)
        {
            var validImageTypes = new string[]
            {
                "image/gif",
                "image/jpeg",
                "image/pjpeg",
                "image/png"
            };

            if ( model.ImageUpload != null && !validImageTypes.Contains(model.ImageUpload.ContentType))
            {
                ModelState.AddModelError("ImageUpload", "請選擇 GIF, JPG or PNG 圖片檔案格式。");
            }

            if (ModelState.IsValid)
            {
                var product = db.Products.Find(id);
                if (product == null)
                {
                    return new HttpNotFoundResult();
                }

                product.Name = model.Name;
                product.Description = model.Description;
                product.Category_Id = model.Category_Id;
                product.Price = model.Price;
                product.Author = model.Author;
                product.Publisher = model.Publisher;
                product.PublishDT = Convert.ToDateTime(model.PublishDT);
                product.IsPublic = model.IsPublic;

                if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0)
                {
                    ImageDeal imageDeal = new ImageDeal();
                    //上傳圖檔&壓縮圖檔處理
                    string imageUrl = imageDeal.Upload(model.ImageUpload);
                    product.PhotoUrl = imageUrl;

                    //刪除舊檔案&壓縮檔案
                    var deletePhotoUrl = Server.MapPath(model.PhotoUrl);
                    imageDeal.Delete(deletePhotoUrl);
                }

                db.Entry(product).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.Category_Id = new SelectList(db.Categories, "Id", "Name", model.Category_Id);
            return View(model);
        }
コード例 #3
0
        // GET: Admin/Products/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Products products = db.Products.Find(id);
            if (products == null)
            {
                return HttpNotFound();
            }
            var model = new ProductsViewModel
            {
                Id = products.Id,
                Name = products.Name,
                Description = products.Description,
                Category_Id = products.Category_Id,
                Price = products.Price,
                PhotoUrl = products.PhotoUrl,
                Author = products.Author,
                Publisher = products.Publisher,
                PublishDT = products.PublishDT,
                IsPublic = products.IsPublic
            };

            ViewBag.Category_Id = new SelectList(db.Categories, "Id", "Name", products.Category_Id);
            return View(model);
        }