コード例 #1
0
        public ActionResult Create(Product product)
        {
            if (ModelState.IsValid)
            {
                context.Products.Add(product);
                context.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.PossibleCategories = context.Categories;
            return View(product);
        }
コード例 #2
0
        // ViewBag & ViewData sample
        public ActionResult Index()
        {
            var featuredProduct = new Product
            {
            Name = "Special Cupcake Assortment!",
            Description = "Delectable vanilla and chocolate cupcakes",
            CreationDate = DateTime.Today,
            ExpirationDate = DateTime.Today.AddDays(7),
            ImageName = "cupcakes.jpg",
            Price = 5.99M,
            QtyOnHand = 12
            };

            ViewData["FeaturedProduct"] = featuredProduct;
            ViewBag.FeaturedProduct = featuredProduct;
            TempData["FeaturedProduct"] = featuredProduct;

            return View();
        }
コード例 #3
0
        // TempData sample
        public ActionResult Featured()
        {
            var featuredProduct = new Product
            {
                Name = "Assorted Cupcakes",
                Description = "Delectable vanilla and chocolate cupcakes",
                CreationDate = DateTime.Today,
                ExpirationDate = DateTime.Today.AddDays(7),
                ImageName = "cupcakes.jpg",
                Price = 5.99M,
                QtyOnHand = 12
            };

            ViewData["FeaturedProduct"] = featuredProduct;
            ViewBag.FeaturedProduct = featuredProduct;
            TempData["FeaturedProduct"] = featuredProduct;

            //After the redirect, the ViewBag & ViewData objects are no longer available
            //Only TempData survives a redirect

            return new RedirectResult(@"~\Featured\");
        }
コード例 #4
0
 public ProductCategoriesViewModel(Product product)
 {
     Product = product;
 }
コード例 #5
0
 public ActionResult Edit(Product product)
 {
     if (ModelState.IsValid)
     {
         context.Entry(product).State = EntityState.Modified;
         context.SaveChanges();
         return RedirectToAction("Index");
     }
     ViewBag.PossibleCategories = context.Categories;
     return View(product);
 }