예제 #1
0
        public async Task <ActionResult> Create(CreateOrderViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Customer customer = await _customerRepository.GetCustomerByNameAsync(model.CustomerName);

                    if (customer == null)
                    {
                        //Ensure there must be a customer
                        ViewBag.Error = "Customer does not Exist, Please Register the customer";
                        return(View("Create", model));
                    }

                    Order order = new Order()
                    {
                        OrderDate = DateTime.Now,
                    };
                    _orderRepository.Add(order);
                    await _orderRepository.SaveAll();

                    await _orderRepository.AddCustomerToOrderAsync(order.OrderID, customer.CustomerID);

                    await _orderRepository.SaveAll();

                    // TODO: Add insert logic here
                    return(RedirectToAction("Index"));
                }
                catch
                {
                    return(View("Create", model));
                }
            }
            else
            {
                return(View("Create", model));
            }
        }