Asp.net mvc 根据第一个液滴列表的值填充第二个液滴列表
如何根据第一个选择的下拉列表值更改第二个下拉列表的选择?例如,在下面的代码中,如果listDepartments是“Sales”,我只希望listCatagory droplist的选项是Customers,而在选择HR时,我只希望选项是“Resumes”,这是否可能与下面的代码一起使用,或者我是否需要一种新的方法,如果是,是否有一个好的示例 谢谢, 电子束 但是我不确定它从哪里获得数据:{id:$(“#droplist_Departments_id”).val()},从哪里来的 我是否需要更改部门的雾滴列表方式Asp.net mvc 根据第一个液滴列表的值填充第二个液滴列表,asp.net-mvc,Asp.net Mvc,如何根据第一个选择的下拉列表值更改第二个下拉列表的选择?例如,在下面的代码中,如果listDepartments是“Sales”,我只希望listCatagory droplist的选项是Customers,而在选择HR时,我只希望选项是“Resumes”,这是否可能与下面的代码一起使用,或者我是否需要一种新的方法,如果是,是否有一个好的示例 谢谢, 电子束 但是我不确定它从哪里获得数据:{id:$(“#droplist_Departments_id”).val()},从哪里来的 我是否需要更改
listDepartments.Add(new SelectListItem
{
Text = "Customer Service",
Value = "CustomerService",
必须使用Javascript才能发出Ajax请求,将上一个droplist所选项目传递给控制器中的方法,该方法将返回
SelectList
,如下所示:
$.ajax({
type: 'POST',
url: 'GetCategory',
dataType: 'json',
data: { id: $("#droplist_Departments_ID").val() },
success: function (mems) {
// states contains the JSON formatted list
// of states passed from the controller
$.each(mems, function (i, member) {
$("#droplist_Category_ID").append('<option value="' + member.Value + '">' + member.Text + '</option>');
});
},
error: function (ex) {
console.log('Failed to retrieve states. Exception: ' + ex);
}
});
listDepartments.Add(new SelectListItem
{
Text = "Customer Service",
Value = "CustomerService",
$.ajax({
type: 'POST',
url: 'GetCategory',
dataType: 'json',
data: { id: $("#droplist_Departments_ID").val() },
success: function (mems) {
// states contains the JSON formatted list
// of states passed from the controller
$.each(mems, function (i, member) {
$("#droplist_Category_ID").append('<option value="' + member.Value + '">' + member.Text + '</option>');
});
},
error: function (ex) {
console.log('Failed to retrieve states. Exception: ' + ex);
}
});
public JsonResult GetCategory(int id)
{
var department = db.Departments.Where(t => t.Id == id).FirstOrDefault();
return Json(new SelectList(db.Categories.Where(t => (t.DepartmentId == department.Id)), "CategoryId", "Text"));
}