2016-08-05 44 views
0

代碼:無法分配/強制轉換的ViewData到變量,對象引用未設置

@{ 
....  
var GotAllDetails = ViewData["GetAllDetails"] as List<PRJ.DetailViewsModel>; 
.... 
} 

@foreach (var goteem in GotAllDetails){Print my pretty list} 

我已經做了很多的調試。我在var GetAllDetails後面放了一個斷點,我可以確認ViewData["GetAllDetails"]已正確填充。我正在類型化的模型具有與ViewData.相同的價值名稱和類型,但是,不知何故,GotAllDetailsnull。當頁面點擊我的foreach它通過我

異常詳細信息:System.NullReferenceException:對象不設置到對象的實例。

根據要求,我會分享更多的代碼,但首先告訴我,如果我有任何明顯的錯誤。

編輯:每請求,這裏的指定計算機[ 「GetAllDetails」]

ViewData["GetAllDetails"] = getAllDetails.OrderBy(q => PadString(q.TaskDescription, ".")).ToList(); 

而且LINQ查詢

var getAllDetails = 
    (from m in _db.MyStandards.AsEnumerable() 
    join t in _db.Tasks on m.TaskId equals t.TaskId 
    join p in _db.Products on t.ProductId equals p.ProductId 
    join ce in _db.CompetencyElements on p.CompetencyElementId equals ce.CompetencyElementId 
    join comp in _db.Competencies on ce.CompetencyId equals comp.CompetencyId 
    join fu in _db.FunctionalUnitOfCompetences on comp.FunUnitOfCompetenceId equals fu.FunUnitOfCompetenceId 
    join c in _db.Careers on fu.CareerId equals c.CareerId 
    join rx in _db.RubricRefs on m.RubricStandardId equals rx.Id 
    where (t.TaskId == m.TaskId && m.Email == getUserById && p.ProductId == proId) 
    group new { t.TaskDescription, m.RubricStandardId, m.Comments } 
    by new 
    { 
     c.CareerDescription, 
     fu.FunUnitOfCompetenceDesc, 
     comp.CompetencyDescription, 
     ce.CompetencyElementdesc, 
     p.ProductDescription, 
     t.TaskDescription, 
     m.RubricStandardId, 
     m.Comments, 
     m.StandardId, 
     m.TaskId, 
     m.ActiveInd, 
     rx.RubricHexColor, 
     rx.RubricSymbol 
    } into g 
    select new 
    { 
     ActiveInd = g.Key.ActiveInd, 
     CareerDescription = g.Key.CareerDescription, 
     Comments = g.Key.Comments, 
     CompetencyDescription = g.Key.CompetencyDescription, 
     CompetencyElementdesc = g.Key.CompetencyElementdesc, 
     FunUnitOfCompetenceDesc = g.Key.FunUnitOfCompetenceDesc, 
     ProductDescription = g.Key.ProductDescription, 
     StandardId = g.Key.StandardId, 
     RubricHexColor = g.Key.RubricHexColor, 
     RubricStandardId = g.Key.RubricStandardId, 
     RubricSymbol = g.Key.RubricSymbol, 
     TaskDescription = g.Key.TaskDescription, 
     TaskId = g.Key.TaskId, 
    }); 

和模型

public class DetailViewsModel 
{ 
    public string ActiveInd { get; set; } 
    public string CareerDescription {get;set;} 
    public string Comments {get;set;} 
    public string CompetencyDescription {get;set;} 
    public string CompetencyElementdesc {get;set;} 
    public string FunUnitOfCompetenceDesc {get;set;} 
    public string ProductDescription {get;set;} 
    public int StandardId {get;set;} 
    public string RubricHexColor {get;set;} 
    public int RubricStandardId {get;set;} 
    public string RubricSymbol {get;set;} 
    public string TaskDescription {get;set;} 
    public int TaskId {get;set;} 
} 
+1

「正確填充」意味着ViewData [「GetAllDetails」]真的返回一個List ?如果返回的對象是不同類型的,則'as'運算符返回'null'。 –

+0

據我所知,是的。 ViewData來自一個LINQ查詢,並且我確信我所輸入的模型具有與所述查詢輸出相同的值。 – UIDAlexD

+1

我的問題不是它具有相同的屬性。你說你調試過它,所以你可以再次調試並確定實際類型。正如我所說,如果它不是正確的類型,「GotAllDetails」將爲空。如果是這種情況,則需要進行某種轉換。 –

回答

2

此代碼:

new { 
    ActiveInd = g.Key.ActiveInd, 
    CareerDescription = g.Key.CareerDescription, 
    Comments = g.Key.Comments, 
    CompetencyDescription = g.Key.CompetencyDescription, 
    CompetencyElementdesc = g.Key.CompetencyElementdesc, 
    FunUnitOfCompetenceDesc = g.Key.FunUnitOfCompetenceDesc, 
    ProductDescription = g.Key.ProductDescription, 
    StandardId = g.Key.StandardId, 
    RubricHexColor = g.Key.RubricHexColor, 
    RubricStandardId = g.Key.RubricStandardId, 
    RubricSymbol = g.Key.RubricSymbol, 
    TaskDescription = g.Key.TaskDescription, 
    TaskId = g.Key.TaskId, 
}); 

...不產生DetailViewsModel。它產生的是一個具有相同屬性名稱和可能的屬性類型的類,但它不會產生SAME類。

如果你想從getAllDetails得到可鑄造成List<DetailViewsModel>,你將不得不實例化一個實際的DetailsViewModel

幸運的是,您已經完成了難題:設置所有屬性值!你應該能夠在這個替代:

new DetailsViewModel() { 
     ActiveInd = etc. 
+0

在這個和其他答案之間進行選擇確實很難,但簡單英語的解釋幫助了很多。謝謝你爲我打破了一切。 – UIDAlexD

0

ViewData的是代碼鏈接到Session。如果你想刪除你的錯誤嘗試這個。

var GotAlLDetails = ViewData["GetAllDetails"]; 

    if(GotAllDetails != null) 
    { 
    // do work 
    } 
+0

這是一個很好的故障保險,但我沒有在市場上進行故障安全。此外,如果我這樣做,我們會因爲'對象不包含GetEnumerator的公共定義而導致編譯時失敗。' – UIDAlexD

1

的問題來自於這句法

var GotAllDetails = ViewData["GetAllDetails"] as List<PRJ.DetailViewsModel>; 

根據MSDN

as運算符是像一個轉換操作。但是,如果轉換不可行,則返回null而不是引發異常。

既然你說ViewData["GetAllDetails"]不爲空,那麼GotAllDetails爲空,因爲ViewData["GetAllDetails"]類型不List<PRJ.DetailViewsModel>。您需要確保ViewData["GetAllDetails"]的類型爲List<PRJ.DetailViewsModel>

基礎上,在你的問題產生getAllDetails如下

var getAllDetails = .... 
        .... 
       select new 
       { 
        ActiveInd = g.Key.ActiveInd, 
        CareerDescription = g.Key.CareerDescription, 
        Comments = g.Key.Comments, 
        CompetencyDescription = g.Key.CompetencyDescription, 
        CompetencyElementdesc = g.Key.CompetencyElementdesc, 
        FunUnitOfCompetenceDesc = g.Key.FunUnitOfCompetenceDesc, 
        ProductDescription = g.Key.ProductDescription, 
        StandardId = g.Key.StandardId, 
        RubricHexColor = g.Key.RubricHexColor, 
        RubricStandardId = g.Key.RubricStandardId, 
        RubricSymbol = g.Key.RubricSymbol, 
        TaskDescription = g.Key.TaskDescription, 
        TaskId = g.Key.TaskId, 
       }); 

LINQ查詢,以及如何設置ViewData["GetAllDetails"]

ViewData["GetAllDetails"] = getAllDetails.OrderBy(q => PadString(q.TaskDescription, ".")).ToList(); 

很明顯的ViewData["GetAllDetails"]類型爲List<anonymous>,而不是List<PRJ.DetailViewsModel>

您需要LINQ查詢更改爲以下

var getAllDetails = .... 
        .... 
       select new DetailViewsModel 
       { 
        ActiveInd = g.Key.ActiveInd, 
        CareerDescription = g.Key.CareerDescription, 
        Comments = g.Key.Comments, 
        CompetencyDescription = g.Key.CompetencyDescription, 
        CompetencyElementdesc = g.Key.CompetencyElementdesc, 
        FunUnitOfCompetenceDesc = g.Key.FunUnitOfCompetenceDesc, 
        ProductDescription = g.Key.ProductDescription, 
        StandardId = g.Key.StandardId, 
        RubricHexColor = g.Key.RubricHexColor, 
        RubricStandardId = g.Key.RubricStandardId, 
        RubricSymbol = g.Key.RubricSymbol, 
        TaskDescription = g.Key.TaskDescription, 
        TaskId = g.Key.TaskId, 
       }); 

然後改變這種

var GotAllDetails = ViewData["GetAllDetails"] as List<PRJ.DetailViewsModel>; 

這個

var GotAllDetails = (List<PRJ.DetailViewsModel>)ViewData["GetAllDetails"]; 

所以如果轉換失敗,你就知道了。

相關問題