1

我正在尝试创建级联下拉列表。我的控制器看起来像这样初始化视图..

  public ActionResult Create()
        {

            var model = new RoundDetailViewModel();


                model.AvailableFacilities = new SelectList(db.Facilities, "FacilityId", "Facility_Name");
                model.AvailableCourses = new SelectList(Enumerable.Empty<Course>(), "CourseId", "Course_Name");
                model.AvailableTeeTypes= new SelectList(Enumerable.Empty<TeeType>(), "TeeTypeId", "Name");


            return View(model);
        }

这将填充第一个下拉列表,并按预期创建 2 个空下拉列表。

现在,在选择第一个下拉列表时,我想在我的控制器中调用一个 Action 来填充第二个下拉列表。这是我对填充第二个下拉列表的操作中的代码有点模糊的地方。我想用这样的东西来触发调用动作..

 $("#ddlFacility").change(function () {
        var selectedFacility = $(this).val();
        if (selectedFacility != null && selectedFacility != '') {
            $.getJSON("@Url.Action("GetCourse")", { facility: selectedFacility }, function (courses) {
                var coursesSelect = $('#ddlCourse');
                coursesSelect.empty();
                $.each(courses, function (index, course) {
                    coursesSelect.append($('<option/>', {
                        value: course.value,
                        text: course.text
                    }));
                });
            });
        }
    });


   public ActionResult Courses(int facilityId)
        {
           //WHAT GOES HERE TO POPULATE SELECT LIST??           

        }
4

1 回答 1

0

需要返回 JsonResult 并允许 Get(如果这是您决定做的)或者您需要将其设为 POST 并对其执行 POST。

就像是

public JsonResult GetCourses(int id)
{        
    var Courses= db.Courses.Where(a=>facilityId==id).ToList();
    SelectList list = new SelectList(Courses,"Text", "Value");
    return Json(list , JsonRequestBehavior.AllowGet);
}
于 2013-03-04T00:42:19.407 回答