2016-08-05 4 views
0

코드를 설정할 수 없습니다. 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 쿼리에서오고 있으며, 내가 typecasting하고있는 모델이 상기 쿼리의 출력과 동일한 값을 가지고 있는지 확인했습니다. – UIDAlexD

+1

동일한 속성을 가진 경우 내 질문이 아니 었습니다. 디버깅했다고해서 다시 디버깅하고 실제 유형을 결정할 수 있습니다. 내가 말했듯이, 그것이 올바른 타입이 아니라면,'GotAllDetails'는 null이 될 것입니다. 그렇다면 어떤 종류의 전환이 필요할 것입니다. –

답변

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 인을 ViewData를 지정하는 코드입니다 세션에 연결되었습니다. 오류를 제거하려면이 시도하십시오.

var GotAlLDetails = ViewData["GetAllDetails"]; 

    if(GotAllDetails != null) 
    { 
    // do work 
    } 
+0

좋은 안전 장치이지만, 나는 안전 장치가 없다고 시장에 나와 있지 않다. 또한,이 작업을 수행하면'Object에 GetEnumerator에 대한 공용 정의가 포함되어 있지 않아 컴파일시 오류가 발생합니다. ' – UIDAlexD

1

문제는 MSDN

에 따르면이 구문

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

에서 온다. 그러나 변환이 불가능한 경우에는 예외가 발생하는 대신 null이 반환됩니다.

당신은 ViewData["GetAllDetails"] 다음, null가 아닌 ViewData["GetAllDetails"]의 유형이 List<PRJ.DetailViewsModel> 아니기 때문에 GotAllDetails가 null 말했다입니다. ViewData["GetAllDetails"]의 유형이 List<PRJ.DetailViewsModel>인지 확인해야합니다.

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

다음과 같이 귀하의 질문에 getAllDetails을 생산 쿼리 및 방법에 따라 당신은 ViewData["GetAllDetails"]

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

ViewData["GetAllDetails"]의 유형 List<anonymous> 대신 List<PRJ.DetailViewsModel> 있음이 분명를 설정합니다.

당신은 변환이 실패 할 경우 그래서 당신은 알 다음이

var GotAllDetails = (List<PRJ.DetailViewsModel>)ViewData["GetAllDetails"]; 

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

을 변경하려면 아래

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

에 LINQ 쿼리를 변경해야합니다.