I am using .net 4.5.1, visual studio 2013. I created an invoice creation page with ViewModel -CreateInvoiceViewModel.
public class CreateInvoiceViewModel
{
public int EntityID { get; set; }
.
.
public ICollection<InvoicePartialCreateMainBillViewModel> MainBill { get; set; }
public ICollection<InvoicePartialCreateDetailBillViewModel> DetailBill { get; set; }
}
When you click Create Invoice through AJAX, a page with a partial view will be loaded on the same page using the ViewModel embedded in the two view models. Nested view models are defined by data in the AJAX function. Partially View ViewModel Page - InvoicePartialCreateViewModel
public class InvoicePartialCreateViewModel
{
public InvoicePartialCreateViewModel()
{
this.MainBill = new HashSet<InvoicePartialCreateMainBillViewModel>();
this.DetailBill = new HashSet<InvoicePartialCreateDetailBillViewModel>();
}
public float TotalAmount { get; set; }
.
.
public ICollection<InvoicePartialCreateMainBillViewModel> MainBill { get; set; }
public ICollection<InvoicePartialCreateDetailBillViewModel> DetailBill { get; set; }
internal void CreateMainBill(int count)
{
for(int i = 0; i < count; i++)
{
this.MainBill.Add(new InvoicePartialCreateMainBillViewModel());
}
}
internal void CreateDetailBill(int count)
{
for (int i = 0; i < count; i++)
{
this.DetailBill.Add(new InvoicePartialCreateDetailBillViewModel());
}
}
}
Nested Models - InvoicePartialCreateMainBillViewModel, InvoicePartialCreateDetailBillViewModel
public class InvoicePartialCreateMainBillViewModel
{
public string PackageName { get; set; }
.
.
public virtual InvoicePartialCreateViewModel InvoiceCreate { get; set; }
}
public class InvoicePartialCreateDetailBillViewModel
{
public DateTime OrderDate { get; set; }
.
.
public virtual InvoicePartialCreateViewModel InvoiceCreate { get; set; }
}
Nested models called html helper @ Html.EditorFor
@Html.EditorFor(model => model.MainBill)
@Html.EditorFor(model => model.DetailBill)
. .
ViewModel Virtual viewModel