public JsonResult Create(BigModelDTO obj) { try { MyRepository repository = new MyRepository(); obj.order.Customer = new CustomerDTO(); if (obj.customer.CustomerId == 0) { obj.order.Customer.CustomerId = repository.AddCustomer(obj.customer); } obj.order.Customer.CustomerId = obj.customer.CustomerId; repository.AddOrder(obj.order); int orderId = obj.order.OrderId; foreach (var orderDetail in obj.orderDetail) { orderDetail.Order = new OrderDTO(); orderDetail.Order.OrderId = orderId; repository.AddOrderDetail(orderDetail); int orderDetailId = orderDetail.OrderDetailId; StockOutDTO stockOut = new StockOutDTO(); stockOut.Date = obj.order.Date; stockOut.Description = "Simple Order"; stockOut.Price = orderDetail.Amount; stockOut.Product = new ProductDTO(); stockOut.Product.ProductId = orderDetail.Product.ProductId; stockOut.Quantity = orderDetail.Quantity; stockOut.OrderDetail = new OrderDetailDTO(); stockOut.OrderDetail.OrderDetailId = orderDetailId; repository.AddStockOut(stockOut); } return(Json("Successfully added", JsonRequestBehavior.AllowGet)); } catch { return(Json("Error while adding", JsonRequestBehavior.AllowGet)); } }