代码:
@{
....
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:未将对象引用设置为对象的实例。
根据要求,我会分享更多代码,但首先告诉我是否有任何明显的错误。
EDIT:Per Request,这是指定ViewData的代码[“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;}
}
答案 0 :(得分: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类。
如果您希望将DetailsViewModel
获得的内容转换为getAllDetails
,则必须实例化List<DetailViewsModel>
。
幸运的是,您已经完成了困难的部分:设置所有属性值!你应该可以用这个代替:
new DetailsViewModel() {
ActiveInd = etc.
答案 1 :(得分: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
的linq查询,如下所示
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,
});
以及如何设置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"];
所以你会知道转换是否失败。
答案 2 :(得分:0)
ViewData链接到Session。如果您想删除错误,请尝试此操作。
app.get('/path', function(req, res) {
if (typeof req.param('param1') !== 'undefined' && typeof req.param('param2') !== 'undefined'){
var param1 = req.param('param1');
var param2 = req.param('param2');
if (param1 === param2) {
// explicitly return
return User.findOne({ 'myfield' : param1 }, function (err, user) {
// Do some async stuff, doesn't matter
})
}
}
return res.redirect('/login');
});