コード例 #1
0
        public ActionResult Create([Bind(Include = "Title,Price,ImageUrl,Category,Artist,Label,Released")] MusicCD musicCD)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.MusicCDs.Add(musicCD);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
            }
            catch (DataException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.

                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }

            return(View(musicCD));
        }
コード例 #2
0
        public ActionResult Create([Bind(Include = "ProductId,Title,Price,ImageUrl,Category,Director")] Movie movie)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    movie.CreatedDate = DateTime.Now;
                    db.Movies.Add(movie);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
            }
            catch (DataException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.
                ModelState.AddModelError("", "Unable to save changes. Try again, and " +
                                         "if the problem persists see your system administrator.");
            }

            return(View(movie));
        }
コード例 #3
0
        public ViewResult Checkout(Cart cart, ShippingDetails shippingDetails)
        {
            if (cart.Lines.Count() == 0)
            {
                ModelState.AddModelError("", "Sorry, your cart is empty!");
            }

            if (ModelState.IsValid)
            {
                Customer customer = new Customer
                {
                    Firstname = shippingDetails.Firstname,
                    Lastname  = shippingDetails.Lastname,
                    Address   = shippingDetails.Address,
                    Zip       = shippingDetails.Zip,
                    Email     = shippingDetails.Email
                };

                if (db.Customers.Any(c => c.Firstname == customer.Firstname && c.Lastname == customer.Lastname && c.Email == customer.Email))
                {
                    customer = db.Customers.Where(c => c.Firstname == customer.Firstname &&
                                                  c.Lastname == customer.Lastname &&
                                                  c.Email == customer.Email).First();
                    customer.Address = shippingDetails.Address;
                    customer.Zip     = shippingDetails.Zip;
                    // ensure update instead of insert
                    db.Entry(customer).State = EntityState.Modified;
                }
                // order processing logic
                Invoice invoice = new Invoice(DateTime.Now, customer);

                foreach (CartLine cartline in cart.Lines)
                {
                    OrderItem orderItem = new OrderItem(cartline.Product, cartline.Quantity);
                    orderItem.ProductId = cartline.Product.ProductId;
                    orderItem.Product   = null;
                    invoice.OrderItems.Add(orderItem);
                }
                db.Invoices.Add(invoice);
                db.SaveChanges();
                cart.Clear();
                return(View("Completed"));
            }

            else
            {
                return(View(shippingDetails));
            }
        }