1

考虑到这个 ViewModel

public class DocumentReferenceViewModel
{
    public int IdDocumentReference { get; set; }
    public string DocumentTitle { get; set; }
    public string PageNbr { get; set; }
    public string Comment { get; set; }
}
  • DocumentTitle 是Document对象的一个​​属性
  • IdDocumentReference和PageNumber是 DocumentReference 对象的属性

DocumentDocumentReference与属性IdDocument链接

  • 我有一个可以返回Document对象的存储库
  • 我有一个可以返回DocumentReference对象的存储库

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 和每个查询数据库以检索 de DocumentTitle 的情况下从这两个对象(表)绑定 ViewModel ?

ps:我没有使用 EF,我有调用手写存储过程的存储库

4

1 回答 1

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 ;
    }
}

您也可以使用 Comment 属性添加构造函数。

public DocumentReferenceViewModel(Document doc, DocumentReference docRef, string comment){

      this.DocumentTitle = doc.DocumentTitle;
      this.IdDocumentReference = docRef.IdDocumentReference;
      this.PageNbr = docRef.PageNbr ;
      this.Comment = comment;
    }

您从存储库中获取DocumentDocumentReference使用新构造函数之一创建一个实例。

于 2012-10-30T09:07:30.017 回答