C# Can';t将/类型转换ViewData分配给变量,未设置对象引用
代码: 我做了很多调试。我在varC# Can';t将/类型转换ViewData分配给变量,未设置对象引用,c#,asp.net,asp.net-mvc,razor,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Razor,Asp.net Mvc 5,代码: 我做了很多调试。我在varGetAllDetails后面放了一个断点,我可以确认ViewData[“GetAllDetails”]已正确填充。我要进行类型转换的模型与ViewData具有所有相同的值名称和类型。然而,不知何故,GotAllDetails是null。当页面点击我的foreach时,它会从我身边经过 异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例 根据请求,我将共享更多代码,但首先告诉我是否犯了任何明显的错误 编辑:根据请
GetAllDetails
后面放了一个断点,我可以确认ViewData[“GetAllDetails”]
已正确填充。我要进行类型转换的模型与ViewData具有所有相同的值名称和类型。
然而,不知何故,GotAllDetails
是null
。当页面点击我的foreach
时,它会从我身边经过
异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例
根据请求,我将共享更多代码,但首先告诉我是否犯了任何明显的错误
编辑:根据请求,下面是指定ViewData[“GetAllDetails”]的代码
和LINQ查询
ViewData["GetAllDetails"] = getAllDetails.OrderBy(q => PadString(q.TaskDescription, ".")).ToList();
模型呢
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,
});
ViewData链接到会话。如果要删除错误,请尝试此操作
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链接到会话。如果要删除错误,请尝试此操作
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;}
}
此代码:
var GotAlLDetails = ViewData["GetAllDetails"];
if(GotAllDetails != null)
{
// do work
}
。。。不会生成DetailViewsModel
。它产生的是一个具有相同属性名称和可能的属性类型的类,但它不产生相同的类
如果希望从getAllDetails
中获得的内容可以转换为列表,则必须实例化一个实际的DetailsViewModel
幸运的是,您已经完成了困难的部分:设置所有属性值!您应该能够在以下情况下替换:
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,
});
此代码:
var GotAlLDetails = ViewData["GetAllDetails"];
if(GotAllDetails != null)
{
// do work
}
。。。不会生成DetailViewsModel
。它产生的是一个具有相同属性名称和可能的属性类型的类,但它不产生相同的类
如果希望从getAllDetails
中获得的内容可以转换为列表,则必须实例化一个实际的DetailsViewModel
幸运的是,您已经完成了困难的部分:设置所有属性值!您应该能够在以下情况下替换:
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,
});
问题来自于这种语法
new DetailsViewModel() {
ActiveInd = etc.
以及如何设置ViewData[“GetAllDetails”]
很明显,ViewData[“GetAllDetails”]
的类型是List
而不是List
您需要将linq查询更改为以下内容
ViewData["GetAllDetails"] = getAllDetails.OrderBy(q => PadString(q.TaskDescription, ".")).ToList();
那就换这个
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”]作为列表;
对此
var GotAllDetails = ViewData["GetAllDetails"] as List<PRJ.DetailViewsModel>;
var GotAllDetails=(List)ViewData[“GetAllDetails”];
因此,您将知道转换是否失败。问题来自此语法
new DetailsViewModel() {
ActiveInd = etc.
以及如何设置ViewData[“GetAllDetails”]
很明显,ViewData[“GetAllDetails”]
的类型是List
而不是List
您需要将linq查询更改为以下内容
ViewData["GetAllDetails"] = getAllDetails.OrderBy(q => PadString(q.TaskDescription, ".")).ToList();
那就换这个
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”]作为列表;
对此
var GotAllDetails = ViewData["GetAllDetails"] as List<PRJ.DetailViewsModel>;
var GotAllDetails=(List)ViewData[“GetAllDetails”];
因此,您将知道转换是否失败。“正确填充”意味着ViewData[“GetAllDetails”]
确实返回一个列表
?如果返回的对象的类型不同,则as
运算符返回null
。据我所知,是的。ViewData来自一个LINQ查询,我确保我输入的模型具有与所述查询的输出相同的值。我的问题不是它是否具有相同的属性。您说您调试了它,所以您可以再次调试并确定真正的类型。正如我所说,如果它不是正确的类型,GotAllDetails
将为空。如果是这种情况,您需要进行某种转换。您说过视图数据来自LINQ查询
,请显示设置视图数据[“GetAllDetails”]
@ekad的控制器代码。现在发布了。“正确填充”意味着ViewData[“GetAllDetails”]
确实返回了列表
?如果返回的对象的类型不同,则as
运算符返回null
。据我所知,是的。ViewData来自一个LINQ查询,我确保我输入的模型具有与所述查询的输出相同的值。我的问题不是它是否具有相同的属性。您说您调试了它,所以您可以再次调试并确定真正的类型。正如我所说,如果它不是正确的类型,GotAllDetails
将为空。如果是这种情况,您需要进行某种转换。您说过视图数据来自LINQ查询
,请显示设置视图数据[“GetAllDetails”]
@ekad的控制器代码。现在贴出来了。这是一个很好的故障保护,但我不想买故障保护。此外,如果我这样做,我们会遇到编译时失败,因为对象不包含GetEnumerator的公共定义。
这是一个很好的故障保护,但我不想买故障保护。此外,如果我这样做,我们会遇到编译时失败,因为对象不包含GetEnumerator的公共定义。
在这个答案和另一个答案之间选择非常困难,但是简单的英语解释帮助很大。谢谢你给我详细解释。在这个答案和另一个答案之间做出选择真的很难,但是简单的英语解释帮助很大。谢谢你帮我把事情说清楚。