private void ButtonSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBoxName.Text)) { MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(textBoxPrice.Text)) { MessageBox.Show("Заполните цену", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(textBoxCount.Text)) { MessageBox.Show("Заполните количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { PriceDetail = Convert.ToInt32(textBoxPrice.Text); logic.CreateOrUpdate(new DetailBindingModel { Id = id, DetailName = textBoxName.Text, Price = Convert.ToInt32(textBoxPrice.Text), TotalAmount = Convert.ToInt32(textBoxCount.Text) }); MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); DialogResult = DialogResult.OK; Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public ActionResult CreateOrder(CreateOrderModel model) { if (!ModelState.IsValid) { ViewBag.OrderCars = _carLogic.Read(null); return(View(model)); } if (model.OrderCars == null) { ViewBag.OrderCars = _carLogic.Read(null); ModelState.AddModelError("", "Выберите автомобиль"); return(View(model)); } var orderCars = new List <OrderCarBindingModel>(); int flag = 0; foreach (var car in model.OrderCars) { if (car.Value > 0) { var cart = _carLogic.Read(new CarBindingModel { Id = car.Key }).FirstOrDefault(); foreach (var det in cart.CarDetails) { var detcount = _detailLogic.Read(new DetailBindingModel { Id = det.Key }).FirstOrDefault(); if ((det.Value.Item2 * car.Value) > detcount.TotalAmount) { //вывести пользователю ModelState.AddModelError("", "МАЛО ДЕТАЛЕЙ"); flag = 1; int raznica = (det.Value.Item2 * car.Value) - detcount.TotalAmount; } else { _detailLogic.CreateOrUpdate(new DetailBindingModel { Id = detcount.Id, DetailName = detcount.DetailName, Price = detcount.Price, TotalAmount = detcount.TotalAmount - (det.Value.Item2 * car.Value) }); } } orderCars.Add(new OrderCarBindingModel { CarId = car.Key, Count = car.Value }); } } if (flag == 1) { _orderLogic.CreateOrUpdate(new OrderBindingModel { ClientId = Program.Client.Id, DateCreate = DateTime.Now, Status = OrderStatus.Ожидает_поставки_деталей, Price = CalculateSum(orderCars), OrderCars = orderCars }); } else { _orderLogic.CreateOrUpdate(new OrderBindingModel { ClientId = Program.Client.Id, DateCreate = DateTime.Now, Status = OrderStatus.Принят, Price = CalculateSum(orderCars), OrderCars = orderCars }); } return(RedirectToAction("Order")); }