Background
I am building a fairly simple Inventory and Order Management System. In all such systems, you eventually get to the bit where an Order (PurchaseOrder
in my case) has a list of Order Line Items (called LineItem
in my application).
Backend Code
For now, my ViewModels map directly to my Entities, and so my PurchaseOrderVm
looks like this:
public class PurchaseOrderVm
{
public PurchaseOrderVm()
{
LineItems = new List<LineItemVm>();
}
public int Id { get; set; }
public DateTime Date { get; set; }
public PurchaseOrderStatus Status { get; set; }
[Display(Name = "Shipping Cost")]
public decimal ShippingCost { get; set; }
[Display(Name = "Import VAT")]
public decimal Vat { get; set; }
[Display(Name = "Import Duty")]
public decimal ImportDuty { get; set; }
[Display(Name = "Supplier Id")]
public string SupplierId { get; set; }
[Display(Name = "Supplier")]
public string SupplierName { get; set; }
public IEnumerable<LineItemVm> LineItems { get; set; }
public List<SelectListItem> Suppliers { get; set; }
public string ButtonText => Id != 0 ? "Update Purchase Order" : "Add Purchase Order";
}
And my LineItemVm
looks like this:
public class LineItemVm
{
public int Id { get; set; }
public int Quantity { get; set; }
public int OrderId { get; set; }
[Display(Name = "Product")]
public int ProductId { get; set; }
}
Front-End / UX
I know the type of experience I want to build, but I'm not sure how much things have moved on with this aspect since MVC3. I want to build this (bits highlighted for importance):
Tried
I was able to get this working for a single LineItem
only by doing this:
<input class="form-control " id="ProductId" name="LineItems[0].ProductId" type="number" value="">
But obviously this is the opposite of dynamic. What is the cleanest way of allowing users to add and remove items as they wish and have the form and model binder still work?