public ActionResult AddItem(int? id) { if (id == null) { return View("Index"); } // DONE: create view model for form and return View with view model AddItemViewModel model = new AddItemViewModel(); // Pass the Order Id through model.OrderId = (int)id; // Load the products into the Product SelectList model.ProductList = new SelectList(productRepository.GetAll(), "Name", "Name"); // Render the view return View(model); }
public ActionResult AddItem(AddItemViewModel addItem) { if (ModelState.IsValid) { // TODO: Get product and order from repositories using view model properties, and add product to order, then redirect to order detail // 1. map view model to model Domain.Order order = orderRepository.Get(addItem.OrderId); Domain.Product product = productRepository.Get(addItem.ProductName); // var orderLineItem = new Domain.OrderLineItem(product, addItem.Quantity); // 2 Add item to the model order.AddOrderLineItem(product, addItem.Quantity); orderRepository.Save(order); // 3 If OK, redirect to OrderDetail return RedirectToAction("OrderDetail", "Orders", new { id=addItem.OrderId }); } else { // TODO: update view model with products data and return View with view model addItem.ProductList = new SelectList(productRepository.GetAll(), "Name", "Name"); } return View(addItem); }