0

我有以下课程:

public class CityViewModel
{
     public City City { get; set; }
     public IList<CityDetail> CityDetails { get; set; }

     public class CityDetail()
     {
         public CityDetail() {
         Text = new HtmlText();
         ImageFile = String.Empty;
         Correct = false;
         Explanation = new HtmlText();
     }
     public bool   Correct { get; set; }
     public HtmlText Text { get; set; }
     public string ImageFile { get; set; }
     public HtmlText Explanation { get; set; }
}

我怎样才能做到这一点,以便当我做类似的事情时:var model = new CityViewModel();

它创建了一个CityViewModel有十个CityDetail记录的?

4

1 回答 1

7

您需要向 CityViewModel 对象添加一个构造函数:

//Add a constructor
public CityViewModel()
{
    //Populate the variable
    CityDetails = Enumerable.Repeat(new CityDetail(), 10).ToList();
}
于 2012-05-08T09:48:08.577 回答