UIDAlexD UIDAlexD - 4 months ago 8
ASP.NET (C#) Question

Can't assign/typecast ViewData to Variable, Object Reference not set

Code:

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


and

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


I've done plenty of debugging. I put a breakpoint right after var
GetAllDetails
, and I can confirm that
ViewData["GetAllDetails"]
is properly populated. The model I'm typecasting to has all the same value names and types as the
ViewData.
Yet, somehow,
GotAllDetails
is
null
. When the page hits my
foreach
it passes me


Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.


Upon request, I'll share more code, but first tell me if I've made any obvious errors.

EDIT: Per Request, here's the code that specifies ViewData["GetAllDetails"]

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


And the LINQ Query

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,
});


And the Model

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;}
}

Answer

This code:

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,
 });

... doesn't yield a DetailViewsModel. What it yields is a class that has the same property names and possibly property types, but it doesn't yield the SAME class.

You're going to have to instantiate an actual DetailsViewModel if you want what you are getting from getAllDetails to be castable to a List<DetailViewsModel>.

Fortunately, you've already done the hard part: setting all the property values! You should be able to substitute in this:

  new DetailsViewModel() {
      ActiveInd = etc.