At the moment my function is posting to an Action in the controller
In the view there is the Ajax function which adds new rows for additional items to be added. Ideally I would like to submit these additional items WITH the model. But I am struggling to do this. In the ViewModel I have a List property:
public ICollection<Item> List_Items { get; set; }
Is it possible to use this to contain ALL the Item related fields?
public class InsertOrderVM
{
//Order table
public int Order_Id { get; set; }
public int Order_num { get; set; }
public string Supplier_Contact_Person { get; set; }
public string Nominal_Code { get; set; }
public string Branch { get; set; }
public string Requested_By { get; set; }
public string Requester_Email { get; set; }
public string Delivery_Address_Line1 { get; set; }
public string Delivery_Address_Line2 { get; set; }
public string Delivery_Address_Town { get; set; }
public string Delivery_Address_County { get; set; }
public string Delivery_Address_Postcode { get; set; }
[Display(Name = "Date Order Placed")]
public DateTime Order_Date { get; set; }
[Display(Name = "Estimated Deliver Date")]
public DateTime Delivery_Date { get; set; }
[Display(Name = "Payment Terms")]
public string Payment_Terms { get; set; }
public string Authorised_By_Id { get; set; }
public bool Requested { get; set; }
//Item table
public int? OrderId_Fk { get; set; }
[ForeignKey("OrderId_Fk")]
public Order Order { get; set; }
public int? SupplierId_Fk { get; set; }
[ForeignKey("SupplierId_Fk")]
public Supplier Supplier { get; set; }
public ICollection<Item> List_Items { get; set; }
public string Item_Name { get; set; }
public string Description { get; set; }
public string Department { get; set; }
public int Quantity { get; set; }
public double Vat_Rate { get; set; }
public double Net_Unit_Price { get; set; }
public double Total_Net { get; set; }
public double Total { get; set; }
//Supplier table
public int Supplier_Id { get; set; }
public string Supplier_Name { get; set; }
public string Supplier_Address_Line1 { get; set; }
public string Supplier_Address_Line2 { get; set; }
public string Supplier_Address_Town { get; set; }
public string Supplier_Address_County { get; set; }
public string Supplier_Address_Postcode { get; set; }
public string Supplier_Phone { get; set; }
public string Supplier_Email { get; set; }
}
[HttpPost]
public async Task<ActionResult> Create(InsertOrderVM model,[FromBody] List<InsertOrderVM> OrderItems)
{
try
{
var orderModel = new InsertOrderVM
{
//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;
var supplierModel = new InsertOrderVM
{
//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;
var itemModel = new InsertOrderVM
{
//item table
OrderId_Fk = _orderID,
SupplierId_Fk= _supplierID,
Item_Name = model.Item_Name,
Description = model.Description,
Department = model.Department,
Quantity = model.Quantity,
Vat_Rate = model.Vat_Rate,
Net_Unit_Price = model.Net_Unit_Price,
Total_Net = model.Total_Net,
};
var requestItem = _mapper.Map<Item>(itemModel);
await _unitOfWork.Items.InsertAndGetIdAsync(requestItem);
await _unitOfWork.Save();
//additional item rows
foreach (InsertOrderVM item in OrderItems)
{
OrderItems.Add(new InsertOrderVM
{
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
});
}
var additionalItems = _mapper.Map<Item>(OrderItems);
await _unitOfWork.Items.InsertAndGetIdAsync(additionalItems);
await _unitOfWork.Save();
return RedirectToAction("Index");
}
catch (Exception ex)
{
ModelState.AddModelError("", "Something went wrong, please try again.");
return View("Index");
//return Json(model);
}
}
<script>
$(document).ready(function () {
var counter = 0;
$("#addrow").on("click", function () {
var newRow = $("<tr>");
var cols = "";
cols += '<td><input id="txtName" type="text" class="form-control" name="txtName' + counter + '" /></td>'
cols += '<td><input id="txtDescription" type="text" class="form-control" name="txtDescription' + counter + '" /></td>';
cols += '<td><select id="ddlDepartment" class="form-control" name="ddlDepartment' + counter + '">';
cols += '<option value="0" selected>Select</option>';
cols += '<option value="1">Department 1</option>';
cols += '<option value="2">Department 2</option>';
cols += '<option value="3">Department 3</option>';
cols += '</select></td >';
cols += '<td><input id="txtQuantity" type="text" class="form-control" name="txtQuantity' + counter + '" /></td>';
cols += '<td><input id="txtVatRate" type="text" class="form-control" name="txtVatRate' + counter + '" /></td>';
cols += '<td><input id="txtNetUnitPrice" type="text" class="form-control" name="txtNetUnitPrice' + counter + '" /></td>';
cols += '<td><input id="txtTotalNet" type="text" class="form-control" name="txtTotalNet' + counter + '" /></td>';
cols += '<td><button type="button" class="ibtnDel btn btn-md btn-danger "><i class="fa fa-trash-o"></i></button></td>';
newRow.append(cols);
$("table.order-list").append(newRow);
counter++;
});
$("table.order-list").on("click", ".ibtnDel", function (event) {
$(this).closest("tr").remove();
counter -= 1
});
$("#btnSubmit").on("click", function () {
var additionItems = new Array();
$("table.order-list TBODY TR").each(function () {
var row = $(this);
var orders = {};
orders.Item_Name = row.find("[id*=txtName]").val();
orders.Description = row.find("[id*=txtDescription]").val();
orders.Department = row.find("[id*=ddlDepartment]").val();
orders.Quantity = row.find("[id*=txtQuantity]").val();
orders.Vat_Rate = row.find("[id*=txtVatRate]").val();
orders.Net_Unit_Price = row.find("[id*=txtNetUnitPrice]").val();
orders.Total_Net = row.find("[id*=txtTotalNet]").val();
additionItems.push(OrderItems);
});
$.ajax({
type: "POST",
url: '@Url.Action("Create", "Order")',
data: JSON.stringify(additionItems),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
alert("inserted.");
},
error: function (r) {
alert(r.responseText);
}
});
});
});
</script>