代码:无法分配/强制转换的ViewData到变量,对象引用未设置
@{
....
var GotAllDetails = ViewData["GetAllDetails"] as List<PRJ.DetailViewsModel>;
....
}
和
@foreach (var goteem in GotAllDetails){Print my pretty list}
我已经做了很多的调试。我在var GetAllDetails
后面放了一个断点,我可以确认ViewData["GetAllDetails"]
已正确填充。我正在类型化的模型具有与ViewData.
相同的价值名称和类型,但是,不知何故,GotAllDetails
是null
。当页面点击我的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;}
}
“正确填充”意味着ViewData [“GetAllDetails”]真的返回一个List?如果返回的对象是不同类型的,则'as'运算符返回'null'。 –
据我所知,是的。 ViewData来自一个LINQ查询,并且我确信我所输入的模型具有与所述查询输出相同的值。 – UIDAlexD
我的问题不是它具有相同的属性。你说你调试过它,所以你可以再次调试并确定实际类型。正如我所说,如果它不是正确的类型,“GotAllDetails”将为空。如果是这种情况,则需要进行某种转换。 –