Does ViewModel support nested models? Is it possible to apply the Virtual property to viewModel?

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

+4
1
  • Viewmodel .

  • ViewModel .

    CreateInvoiceViewModel {   public int EntityID {get; ; }

    public IList<InvoicePartialCreateMainBillViewModel> MainBill { get; set; }
    
    public IList<InvoicePartialCreateDetailBillViewModel> DetailBill { get; set; }
    

    }

    , viewmodels.

    , .

    , A 1 2. A, . A .

    :

    public class BaseViewModel
    {           
       [Required]
       public virtual int propA { get; set;}           
    }
    
    public class DerivedViewModel1
    {
        ......
    }
    
    public class DerivedViewMode2
    {     
        public override int propA { get; set; }
    }
    

    , .

+1

Source: https://habr.com/ru/post/1623744/


All Articles