我有以下课程:
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();
它会创建一个包含10个CityViewModel
条记录的CityDetail
答案 0 :(得分:7)
您需要向CityViewModel对象添加构造函数:
//Add a constructor
public CityViewModel()
{
//Populate the variable
CityDetails = Enumerable.Repeat(new CityDetail(), 10).ToList();
}