0

コントローラーにアクセスしていますが、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
                        }));
                    });
                });
            }
            });
4

1 に答える 1

0

カスケード ドロップダウンの読み込み中にスクリプト エラーを見つけてみてください。うまくいかなかった場合は、別の方法で値をロードしてみてください。私は通常、カスケードドロップダウンリストをロードするために以下のアプローチに従います。

[脚本]

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

                    });
                }
            });
        });

  }

[コントローラ]

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

}

于 2013-03-18T04:36:30.987 に答える