考虑此ViewModel
public class DocumentReferenceViewModel
{
public int IdDocumentReference { get; set; }
public string DocumentTitle { get; set; }
public string PageNbr { get; set; }
public string Comment { get; set; }
}
文档和 DocumentReference 与属性 IdDocument
相关联public class DocumentReference
{
public int IdDocumentReference;
public string PageNbr { get; set; }
public string Comment { get; set; }
}
public class Document
{
public int IdDocument;
public string DocumentTitle { get; set; }
}
如何在没有循环DocumentReference的情况下从这两个对象(表)绑定ViewModel,并且每个查询数据库以检索de DocumentTitle?
ps:我没有使用EF,我有调用手写存储过程的存储库
答案 0 :(得分:0)
您可以在ViewModel中使用构造函数:
public class DocumentReferenceViewModel
{
public int IdDocumentReference { get; set; }
public string DocumentTitle { get; set; }
public string PageNbr { get; set; }
public string Comment { get; set; }
public DocumentReferenceViewModel(){}
public DocumentReferenceViewModel(Document doc, DocumentReference docRef){
this.DocumentTitle = doc.DocumentTitle;
this.IdDocumentReference = docRef.IdDocumentReference;
this.PageNbr = docRef.PageNbr ;
}
}
您还可以添加带注释属性的构造函数。
public DocumentReferenceViewModel(Document doc, DocumentReference docRef, string comment){
this.DocumentTitle = doc.DocumentTitle;
this.IdDocumentReference = docRef.IdDocumentReference;
this.PageNbr = docRef.PageNbr ;
this.Comment = comment;
}
您从存储库Document
和DocumentReference
获取并使用新构造函数之一创建实例。