0

私は次のコードを持っています。

エラーは次の行にあります。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());  
}
4

1 に答える 1

2

/ブロックtestinstances内でのみ宣言しましたが、外部で使用しようとしています。次のように、の外側で宣言してみてください。ifelse

// 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());  
于 2013-10-15T01:17:11.160 に答える