2012-10-28 54 views
0

我父類我的模型:錯誤使用類的模型時,而不是使用元組

public class MyModel 
{ 
    public ThemesModel themesModel { get; set; } 
    public ThemeGroupsModel themesGroupsModel { get; set; } 
    public ThemeTypsModel themesTypesModel { get; set; } 
} 

我用它在我的方法:

public List<MyModel> getAllTheme() 
{ 
    List<MyModel> myModelList = new List<MyModel>(); 
    MyModel myModelTemp; 
    using (var context = new EShopThemeDBEntities(idbconnection.ConnStr)) 
    { 
     var ThemeTypeQ = (//some queries...).ToList(); 

     if (ThemeTypeQ != null) 
     { 
      foreach (var item in ThemeTypeQ) 
      { 
        myModelTemp = new MyModel(); 
        myModelTemp.themesTypesModel.ThemeTypeName = item.ThemeTypeName; 
         . 
         . 
         . 
      } 
     } 
    } 

    return myModelList; 
} 

我就在這行錯誤:

myModelTemp.themesTypesModel.ThemeTypeName = item.ThemeTypeName;

錯誤:

Object reference not set to an instance of an object.

我已經使用Tuple<ThemeTypsModel, ThemesModel, ThemeGroupsModel>而不是使用MyModel和我的項目已經工作過。

我該怎麼做才能解決它?

回答

1

我解決它自己通過改變MyModel.cs這樣:

public class MyModel 
{ 
public ThemesModel themesModel { get; set; } 
public ThemeGroupsModel themesGroupsModel { get; set; } 
public ThemeTypsModel themesTypesModel { get; set; } 

    public MyModel() 
    { 
     themesModel = new ThemesModel(); 
     themesGroupsModel = new ThemeGroupsModel(); 
     themesTypesModel = new ThemeTypsModel(); 
    } 

}

我不要刪除我的問題由其他人誰也有同樣問題的使用。

相關問題