2012-05-08 27 views
0

我有以下類:我創建一個課程時如何填充IList?

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();

它創建一個CityViewModelCityDetail記錄?

回答

7

你需要一個構造函數添加到您的CityViewModel對象:

//Add a constructor 
public CityViewModel() 
{ 
    //Populate the variable 
    CityDetails = Enumerable.Repeat(new CityDetail(), 10).ToList(); 
} 
相關問題