我正在使用.net 4.5.1,visual studio 2013.
我使用viewmodel -CreateInvoiceviewmodel创建了一个发票创建页面.
我使用viewmodel -CreateInvoiceviewmodel创建了一个发票创建页面.
public class CreateInvoiceviewmodel { public int EntityID { get; set; } . . public ICollection<InvoicePartialCreateMainBillviewmodel> MainBill { get; set; } public ICollection<InvoicePartialCreateDetailBillviewmodel> DetailBill { get; set; } }
单击“生成发票”,通过AJAX,部分视图页面将与viewmodel一起加载到同一页面中,viewmodel与另外两个View Models嵌套.
嵌套的View Models在AJAX中被赋予数据,称为函数.
部分查看页面viewmodel – 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()); } } }
嵌套模型 – 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; } }
嵌套模型由html helper @ Html.EditorFor调用
@Html.EditorFor(model => model.MainBill) @Html.EditorFor(model => model.DetailBill)
解决方法
> viewmodel支持嵌套模型.
>虽然不建议在viewmodel中使用虚拟.
>虽然不建议在viewmodel中使用虚拟.
公共类CreateInvoiceviewmodel
{
public int EntityID {get;组; }
public IList<InvoicePartialCreateMainBillviewmodel> MainBill { get; set; } public IList<InvoicePartialCreateDetailBillviewmodel> DetailBill { get; set; }
}
Virtual通常与域对象一起使用,域对象最终用于填充或映射视图模型.
虽然,它可能完全取决于您的方案.
例如,您有一个具有属性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; } }
在这种情况下,您可以使用虚拟,否则不建议使用虚拟.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。