[英]Second dropdown of cascading dropdown is not populating
我碰到了控制器,但是當我回到javascript時, $.getJSON()
函數中嵌入的function (courses)
被跳過,調試器移至$.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
}));
});
});
}
});
加載級聯下拉菜單時,嘗試查找腳本錯誤。 如果無法實現,請嘗試另一種方法來加載值。 我通常按照以下方法加載級聯下拉列表。
[腳本]
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.