コントローラーにアクセスしていますが、JavaScriptに戻るとfunction (courses)
、関数に埋め込まれている$.getJSON()
ものがスキップされ、デバッガーが$.getJSON()
関数の最後に移動し、2番目のドロップダウンリスト(コースのリスト)のデータが入力されていません。理由はわかりません。
これが私のコントローラーです:
public JsonResult GetCourses(int facilityId)
{
return Json(GetCoursesSelectList(facilityId), JsonRequestBehavior.AllowGet);
}
private SelectList GetCoursesSelectList(int id)
{
var Courses = db.Courses.Distinct().Where(a => a.FacilityId == id).ToList();
SelectList list = new SelectList(Courses);
return list;
}
私のJavaScriptは次のとおりです。
$("#ddlFacilities").change(function () {
var selectedFacility = $(this).val();
if (selectedFacility !== null && selectedFacility !== '') {
$.getJSON("/RoundDetail/GetCourses", { FacilityId: selectedFacility },
function (courses) {
alert(course.Course_Name);
var coursesSelect = $('#ddlCourse');
coursesSelect.empty();
$.each(courses, function (index, course) {
coursesSelect.append($('<option/>', {
value: course.CourseId,
text: course.Course_Name
}));
});
});
}
});