コード例 #1
0
        public ActionResult CreateProduct(HttpPostedFileBase file,Product model)
        {
            SmartNerdDataContext db = new SmartNerdDataContext();

            if(file == null || file.ContentLength == 0 || file.ContentType != "image/png") {
                ModelState.AddModelError("","Product Image must be a valid PNG");
                TempData["ModelState"] = ModelState;
            }

            if(ModelState.IsValid) {
                var prod = new DataModels.Product {
                    Description = model.Description,
                    Inventory = model.Inventory,
                    Name = model.ProductName,
                    Price = model.Price
                };

                db.Products.InsertOnSubmit(prod);
                db.SubmitChanges();

                var path = Path.Combine(Server.MapPath("~/Images/p"),prod.ProductID.ToString() + ".png");
                file.SaveAs(path);
                return RedirectToAction("Product",new { productID = prod.ProductID });
            }

            return RedirectToAction("CreateProduct",model);
        }
コード例 #2
0
        public ActionResult DeleteProduct(Product model)
        {
            SmartNerdDataContext db = new SmartNerdDataContext();

            var entries = (from a in db.CategoryEntries
                           where a.ProductID == model.ProductID
                           select a).ToList();

            foreach(var entry in entries) db.CategoryEntries.DeleteOnSubmit(entry);

            var prod = db.Products.Single(a => a.ProductID == model.ProductID);
            db.Products.DeleteOnSubmit(prod);

            db.SubmitChanges();

            return RedirectToAction("Browse");
        }
コード例 #3
0
        public ActionResult UploadImage(HttpPostedFileBase file,Product model)
        {
            if(file != null && file.ContentLength > 0 && file.ContentType == "image/png") {
                var path = Path.Combine(Server.MapPath("~/Images/p"),model.ProductID.ToString() + ".png");
                file.SaveAs(path);
            } else {
                ModelState.AddModelError("","Product Image must be a valid PNG");
                TempData["ModelState"] = ModelState;
            }

            return RedirectToAction("Product",model);
        }
コード例 #4
0
        public ActionResult Product(Product model)
        {
            SmartNerdDataContext db = new SmartNerdDataContext();

            var prod = db.Products.Single(a => a.ProductID == model.ProductID);
            prod.Description = model.Description;
            prod.Name = model.ProductName;
            prod.Price = model.Price;
            prod.Inventory = model.Inventory;

            db.SubmitChanges();

            return View(model);
        }