Javascript 级联下拉列表的第二个下拉列表未填充

Javascript 级联下拉列表的第二个下拉列表未填充,javascript,jquery,asp.net-mvc-4,Javascript,Jquery,Asp.net Mvc 4,我正在点击控制器,但当我回到javascript时,$函数中嵌入的函数(课程)。getJSON()函数被跳过,调试器转到$。getJSON()函数的末尾,第二个下拉列表(课程列表)的数据没有填充。我不知道为什么 这是我的控制器: public JsonResult GetCourses(int facilityId) { return Json(GetCoursesSelectList(facilityId), JsonRequestBehavior.AllowGet); } priv

我正在点击控制器,但当我回到javascript时,
$函数中嵌入的
函数(课程)
。getJSON()
函数被跳过,调试器转到
$。getJSON()
函数的末尾,第二个下拉列表(课程列表)的数据没有填充。我不知道为什么

这是我的控制器:

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
                        }));
                    });
                });
            }
            });
$(“#ddl设施”).change(函数(){
var selectedFacility=$(this.val();
if(selectedFacility!==null&&selectedFacility!==''){
$.getJSON(“/RoundDetail/GetCourses”,{FacilityId:selectedFacility},
功能(课程){
警报(课程。课程名称);
var courseselect=$(“#ddlCourse”);
courseselect.empty();
$。每个(课程、功能(索引、课程){
课程选择。附加($(''){
值:course.CourseId,
文本:course.course\u Name
}));
});
});
}
});

加载级联下拉列表时,尝试查找脚本错误。如果无法加载,请尝试另一种加载值的方法。我通常按照下面的方法加载级联dropdownlist

[文字]

function SetdropDownData(sender, args) {           

        $('#ShipCountry').live('change', function () {
            $.ajax({
                type: 'POST',
                url: 'Home/GetCities',
                data: { Country: $('#ShipCountry').val() },
                dataType: 'json',
                success: function (data) {
                    $('#ShipCity option').remove();
                    $.each(data, function (index, val) {                           
                            var optionTag = $('<option></option>');
                            $(optionTag).val(val.Value).text(val.Text);
                            $('#ShipCity').append(optionTag);

                    });
                }
            });
        });

  }
函数SetdropDownData(发送方,参数){
$(“#ShipCountry”).live('change',function(){
$.ajax({
键入:“POST”,
url:“主页/获取城市”,
数据:{Country:$('#ShipCountry').val()},
数据类型:“json”,
成功:功能(数据){
$(“#ShipCity选项”).remove();
$.each(数据,函数(索引,val){
var optionTag=$('');
$(optionTag).val(val.Value).text(val.text);
$(#ShipCity')。附加(optionTag);
});
}
});
});
}
[控制器]

public IEnumerable<SelectListItem> Cities(string Country)  // ShipCity is filtered based on the ShipCountry value  
    {
        var Cities = new NorthwindDataContext().Orders.Where(c=>c.ShipCountry == Country).Select(s => s.ShipCity).Distinct().ToList();
        List<SelectListItem> type = new List<SelectListItem>();
        foreach (var city in Cities)
        {
            if (city != null)
            {
                SelectListItem item = new SelectListItem() { Text = city.ToString(), Value = city.ToString() };
                type.Add(item);
            }
        }
        return type;
    }
    public ActionResult GetCities(string Country)  
    {
        return Json(Cities(Country), JsonRequestBehavior.AllowGet);
public IEnumerable Cities(字符串国家)//根据ShipCountry值筛选ShipCity
{
var Cities=new NorthwindDataContext().Orders.Where(c=>c.ShipCountry==Country).选择(s=>s.ShipCity).Distinct().ToList();
列表类型=新列表();
foreach(城市中的var城市)
{
如果(城市!=null)
{
SelectListItem=new SelectListItem(){Text=city.ToString(),Value=city.ToString()};
类型。添加(项目);
}
}
返回类型;
}
公共行动结果GetCities(字符串国家/地区)
{
返回Json(城市(国家),JsonRequestBehavior.AllowGet);

}

使用浏览器开发工具(例如:firebug)并检查在请求时间时出现的错误…我正在使用Chrome中的浏览器开发工具。你所说的请求时间是什么意思?你的JSON调用返回一个错误(失败),因此它跳过了success方法。检查chrome控制台中的网络/XHR。如果有错误,当您启动dropdownlist更改方法时,chrome会告诉您您遇到了什么错误。可能是区分大小写的错误。在脚本代码
FacilityId
和控制器
FacilityId