Hi
I am passing model to controller for insertion. With model is the Icollection<Items>List_Items.
How to display the List along with model to details page.
When the POST has happened the details page must then load with the form data, plus the multiple items from the form.
How can I add the List_Items to the model being returned to the Details page as per my code here:
// POST: OrderController/Create
[HttpPost]
public async Task<ActionResult> Create([FromBody] InsertOrderVM model)
{
try
{
var orderModel = new InsertOrderVM
{
//add to order table
Supplier_Contact_Person = model.Supplier_Contact_Person,
Branch = model.Branch,
Requested_By = model.Requested_By,
Delivery_Address_Line1 = model.Delivery_Address_Line1,
Delivery_Address_Line2 = model.Delivery_Address_Line2,
Delivery_Address_County = model.Delivery_Address_County,
Delivery_Address_Postcode = model.Delivery_Address_Postcode,
Order_Date = model.Order_Date,
Delivery_Date = model.Delivery_Date,
Payment_Terms = model.Payment_Terms,
Requested = true
};
var requestOrder = _mapper.Map<Order>(orderModel);
await _unitOfWork.Orders.InsertAndGetIdAsync(requestOrder);
await _unitOfWork.Save();
var _orderID = requestOrder.Order_Id;
////update model with the order_id
//orderModel.Order_Id = requestOrder.Order_Id;
var supplierModel = new InsertOrderVM
{
//add to supplier table
Supplier_Name = model.Supplier_Name,
Supplier_Address_Line1 = model.Supplier_Address_Line1,
Supplier_Address_Line2 = model.Supplier_Address_Line2,
Supplier_Address_Town = model.Supplier_Address_Town,
Supplier_Address_County = model.Supplier_Address_County,
Supplier_Address_Postcode = model.Supplier_Address_Postcode,
Supplier_Phone = model.Supplier_Phone,
Supplier_Email = model.Supplier_Email
};
var requestSupplier = _mapper.Map<Supplier>(supplierModel);
await _unitOfWork.Suppliers.InsertAndGetIdAsync(requestSupplier);
await _unitOfWork.Save();
var _supplierID = requestSupplier.Supplier_Id;
//update model with the order_id
orderModel.Order_Id = requestOrder.Order_Id;
//add to items table
foreach (Item item in model.List_Items)
{
var itemModel = new InsertOrderVM
{
//loop through each field
OrderId_Fk = _orderID,
SupplierId_Fk = _supplierID,
Item_Name = item.Item_Name,
Description = item.Description,
Department = item.Department,
Quantity = item.Quantity,
Vat_Rate = item.Vat_Rate,
Net_Unit_Price = item.Net_Unit_Price,
Total_Net = item.Total_Net
};
var requestItem = _mapper.Map<Item>(itemModel);
await _unitOfWork.Items.InsertAndGetIdAsync(requestItem);
await _unitOfWork.Save();
orderModel.List_Items = itemModel.List_Items;
}
//Redirect to details page handled by ajax function
return Json(orderModel);
}
catch (Exception ex)
{
ModelState.AddModelError("", "Something went wrong, please try again.");
return View("Index");
}
}
// GET: OrderController/Details/5
public async Task<ActionResult> Details(int id)
{
var orderRequest = await _unitOfWork.Orders.Find(q => q.Order_Id == id);
var model = _mapper.Map<InsertOrderVM>(orderRequest);
//get the items for this order_id and add to the model being returned for details page
return View(model);
}
The details page loads, but the List_Items is not being passed. I have a foreach loop to loop through List_Items but returns null so it is not being passed back.
Please help thanks.