2016-08-05 19 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"]; 

所以如果转换失败,你就知道了。

相关问题