コード例 #1
0
        public async Task <IActionResult> Create(Order viewModel)
        {
            var items = await _shoppingCart.GetShoppingCartItems();

            _shoppingCart.ShoppingCartItems = items;
            shoppingCartViewModel           = new ShoppingCartVm
            {
                ShoppingCart      = _shoppingCart,
                ShoppingCartTotal = await _shoppingCart.GetShoppingCartTotal()
            };

            if (_shoppingCart.ShoppingCartItems.Count == 0)
            {
                ModelState.AddModelError("", "Your cart is empty.");
            }

            if (ModelState.IsValid)
            {
                var shoppingCartItems = _shoppingCart.ShoppingCartItems;
                viewModel.OrderTotal = await _shoppingCart.GetShoppingCartTotal();

                await _orderService.Add(viewModel, shoppingCartItems);

                await _shoppingCart.ClearCart(HttpContext.Session.GetString("CartId"));

                return(RedirectToAction(nameof(Index)));
            }

            if (User.IsInRole(Roles.Client))
            {
                return(View(viewModel));
            }
            return(View("~/Views/Checkout/ManageCreate.cshtml", viewModel));
        }