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オブジェクトのプロパティです
  • IdDocumentReferencePageNumberは 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 をループせずにこれらの 2 つのオブジェクト (テーブル) から 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;
    }

リポジトリから取得し、Document新しいDocumentReferenceコンストラクターの 1 つを使用してインスタンスを作成します。

于 2012-10-30T09:07:30.017 に答える