2013-10-15 6 views
0

다음 코드가 있습니다.'testinstances'이름이 현재 컨텍스트에 없습니다.

오류는이 라인에 있습니다 if (testinstances == null)

이름의 testinstances은 현재 컨텍스트에 존재하지 않습니다.

이 오류의 원인은 무엇입니까?

public ActionResult Index(int? classRoomId, int? courseId, int? testTypeId) 
{ 
    var classRoom = cls.GetAll(); 
    var course = cos.GetAll(); 
    var testType = tst.GetAll(); 

    ViewBag.ClassRoomID = new SelectList(classRoom, "ClassRoomID", "ClassRoomTitle"); 
    ViewBag.CourseID = new SelectList(course, "CourseID", "Title"); 
    ViewBag.TestTypeID = new SelectList(testType, "TestTypeID", "TestTypeDesc"); 


    if (classRoomId == null || courseId == null || testTypeId == null) 
    { 
     var testinstances = tt.GetAll(); 
    } 
    else 
    { 

     var testinstances = tt.GetAll().Where(t => t.TestTypeID == testTypeId && 
               t.ClassRoomID == classRoomId && 
               t.CourseID == courseId); 
    } 

    if (testinstances == null) 
    { 
     throw new ArgumentNullException("No Test Found.Do you want to create one?"); 

     RedirectToAction("Create"); 

    } 
    return View(testinstances.ToList()); 
} 

답변

1

는 경우에만 if/else 블록 내에서 testinstances을 선언했지만, 당신은 외부를 사용하려는.

// Note, you must explicitly declare the data type if you use this method 
IQueryable<SomeType> testinstances; 

if (classRoomId == null || courseId == null || testTypeId == null) 
{ 
    testinstances = tt.GetAll(); 
} 
else 
{ 
    testinstances = tt.GetAll().Where(t => t.TestTypeID == testTypeId && 
             t.ClassRoomID == classRoomId && 
             t.CourseID == courseId); 
} 

if (testinstances == null) 
{ 
    throw new ArgumentNullException("No Test Found.Do you want to create one?"); 
    RedirectToAction("Create"); 
} 
return View(testinstances.ToList()); 

을 아니면 조금 청소기 :이처럼 외부로 선언하십시오

var testinstances = tt.GetAll(); 

if (classRoomId != null && courseId != null && testTypeId != null) 
{ 
    testinstances = testinstances.Where(t => t.TestTypeID == testTypeId && 
             t.ClassRoomID == classRoomId && 
             t.CourseID == courseId); 
} 

if (testinstances == null) 
{ 
    throw new ArgumentNullException("No Test Found.Do you want to create one?"); 
    RedirectToAction("Create"); 
} 
return View(testinstances.ToList()); 
+0

감사 P.S.W.g 당신의 대답. 당신의 대답으로, 나는 testinstances를 선언했고 그것은 작동합니다. 당신은 A + –

+0

@frank입니다. 그의 대답을 수락하십시오. – dcaswell

관련 문제