public ActionResult OrderDetails(int id) { Order order; List<Order_Detail> orderDetail; Customer customer; List<Product> products; List<Category> categories; using (NorthwindConnection db = new NorthwindConnection()) { order = db.Orders.SingleOrDefault(o => o.OrderID == id); orderDetail = GetOrderDetails(db, order.OrderID); customer = db.Customers.SingleOrDefault(c => c.CustomerID == order.CustomerID); products = db.Products.ToList(); categories = db.Categories.ToList(); } OrderDetailsModel model = new OrderDetailsModel(); model.Order = order; model.OrderDetails = orderDetail; model.Customer = customer; model.Products = products; model.categories = categories.Select(x => new SelectListItem() { Value = x.CategoryID.ToString(), Text = x.CategoryName }); return PartialView("_OrderDetails", model); }
public ActionResult FindProducts(OrderDetailsModel model, int id) { List<Product> products; using (NorthwindConnection db = new NorthwindConnection()) { products = db.Products.Where(x => x.CategoryID == model.Product.CategoryID && x.UnitPrice>=model.MinPrice).ToList(); if (model.MaxPrice > 0) { products = products.Where(x => x.UnitPrice <= model.MaxPrice).ToList(); } if (model.Product.ProductName != null && model.Product.ProductName!="") { products = products.Where(x=>x.ProductName.Contains(model.Product.ProductName)).ToList(); } } ProductsListModel plModel = new ProductsListModel { Products = products, OrderId = id }; return PartialView("_ProductList", plModel); }
public ActionResult ChangeAmount(int orderId, int productId, string change, short amount) { Order order; List<Order_Detail> orderDetail; Customer customer; List<Product> products; List<Category> categories; using (NorthwindConnection db = new NorthwindConnection()) { categories = db.Categories.ToList(); products = db.Products.ToList(); order = db.Orders.SingleOrDefault(o => o.OrderID == orderId); orderDetail = GetOrderDetails(db, order.OrderID); short quantity = orderDetail.SingleOrDefault(x => x.ProductID == productId).Quantity; if (change == "add") { quantity += amount; } else { if ((quantity - amount) > 0) { quantity -= amount; } else { quantity = 0; } } if (quantity > 0) { orderDetail.SingleOrDefault(x => x.ProductID == productId).Quantity = quantity; } else { Order_Detail rowToRemove = orderDetail.SingleOrDefault(x => x.ProductID == productId); orderDetail.Remove(rowToRemove); db.DeleteOrderDetail(order.OrderID, productId); } customer = db.Customers.SingleOrDefault(c => c.CustomerID == order.CustomerID); db.SaveChanges(); } OrderDetailsModel model = new OrderDetailsModel(); model.Order = order; model.OrderDetails = orderDetail; model.Customer = customer; model.Products = products; model.categories = categories.Select(x => new SelectListItem() { Value = x.CategoryID.ToString(), Text = x.CategoryName }); return PartialView("_OrderDetails", model); }