public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { HttpContextBase httpContext = controllerContext.HttpContext; Customer customer = new Customer(); customer.CustomerName = httpContext.Request["CustomerName"].ToString(); customer.CustomerCode = httpContext.Request["CustomerCode"].ToString(); return customer; }
public List<Customer> Put(Customer oCustomer) { CustomerDB db = new CustomerDB(); if (ModelState.IsValid) { Customer customer = (from x in db.Customers where x.CustomerCode == oCustomer.CustomerCode select x).Single<Customer>(); customer.CustomerAmount = oCustomer.CustomerAmount; customer.CustomerName = oCustomer.CustomerName; List<Order> customerOrders = (from x in db.Orders where x.CustomerCode == oCustomer.CustomerCode select x).ToList<Order>(); foreach (Order order in oCustomer.CustomerOrders) { //check if order exists than update else insert Order currentOrder = customerOrders.Find(i => i.OrderId == order.OrderId); if (currentOrder != null) { //update currentOrder.OrderNumber = order.OrderNumber; currentOrder.Amount = order.Amount; } else { //insert db.Orders.Add(new Order { OrderNumber = order.OrderNumber, CustomerCode = order.CustomerCode, Amount = order.Amount }); } } db.SaveChanges(); } return db.Customers.ToList<Customer>(); }
public List<Customer> Post(Customer oCustomer) { CustomerDB db = new CustomerDB(); if (ModelState.IsValid) { db.Customers.Add(oCustomer); foreach (Order order in oCustomer.CustomerOrders) { db.Orders.Add(new Order { OrderNumber = order.OrderNumber, CustomerCode = order.CustomerCode, Amount = order.Amount }); } db.SaveChanges(); } return db.Customers.ToList<Customer>(); }