Пример #1
0
        public ActionResult Edit(Orders.Models.ViewModelOrder model)
        {
            try
            {
                using (OrdersContext context = new OrdersContext())

                {
                    // проверка, не изменился ли клиент:
                    Customer customer =
                        context.Customer.FirstOrDefault(c =>
                                                        c.Name.Equals(model.Customer, StringComparison.CurrentCultureIgnoreCase));

                    // создаем нового клиента, если изменился:
                    if (customer == null)
                    {
                        customer = new Customer {
                            Name = model.Customer
                        };
                        context.Customer.Add(customer);
                        context.SaveChanges();
                    }

                    SalesOrder order = context.SalesOrder.FirstOrDefault(o => o.Id == model.Id);
                    if (order != null)
                    {
                        order.OrderDate  = DateTime.Now;
                        order.Comment    = model.Comment;
                        order.StatusId   = GetOrderStatusId(context, model.Status);
                        order.CustomerId = customer.Id;
                        context.SaveChanges();
                    }
                    else
                    {
                        //todo - inform client
                    }

                    return(RedirectToAction(nameof(Index)));
                }
            }
            catch
            {
                return(View());
            }
        }
Пример #2
0
        public ActionResult Create(Orders.Models.ViewModelOrder model)
        {
            try
            {
                using (OrdersContext context = new OrdersContext())

                {
                    Customer customer =
                        context.Customer.FirstOrDefault(c =>
                                                        c.Name.Equals(model.Customer, StringComparison.CurrentCultureIgnoreCase));

                    if (customer == null)
                    {
                        customer = new Customer {
                            Name = model.Customer
                        };
                        context.Customer.Add(customer);
                        context.SaveChanges();
                    }

                    context.SalesOrder
                    .Add(new SalesOrder
                    {
                        CustomerId = customer.Id,
                        Comment    = model.Comment,
                        OrderDate  = DateTime.Now,
                        StatusId   = GetOrderStatusId(context, "подтвержден")
                    });

                    context.SaveChanges();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            catch
            {
                return(View());
            }
        }