ASP.NET MVC 中的依赖下拉列表
Dependent dropdownlist in ASP.NET MVC
目前我有两个表(团队和员工)
我正在完美地填充 Teams 的下拉列表,接下来我尝试根据员工的 Teams 的 selectedId 填充第二个下拉列表。
控制器:
// GET: CalView
public ActionResult Index(string ses, string DH)
{ //Team Lead Members
var eID = Convert.ToInt32(Session["currentEmployeeID"]);
var EmpID = Session["currentEmpID"];
Employee obj = (from o in db.Employees
where o.EnrollNumber == EmpID
select o).FirstOrDefault();
Department dept = (from dep in db.Departments
where dep.LeadBy == obj.EmployeeId
select dep).FirstOrDefault();
//this works fine
ViewBag.showTeams = new SelectList(db.Teams.Where(tm => (tm.DeptID == dept.DepartmentId) && (dept.LeadBy == eID)), "TeamID","Name");
//this obviously does not
ViewBag.showMembers = new SelectList(db.Employees.Where(empt => (empT.TeamID == selectedIdFromPreviousDropDownList), "EmployeeID", "Employee"));
return View();
}
查看:
if ((Session["UT"] == "DD") && (@ViewBag.DeptLead != null))
{
//this works
@Html.DropDownList("showTeams", null, "-Select Team-", htmlAttributes: new { @class = "form-control" })
//this does not work
@Html.DropDownList("showMembers", null, "-Select Team-", htmlAttributes: new { @class = "form-control" })
}
我需要一些 AJAX 电话吗?或者 POST 方法?对 MVC 完全陌生。
我需要一些 AJAX 电话吗?或者 POST 方法? 好吧,让我们这样做:
给你的 DropdownLists 一些可能的 id:
@Html.DropDownList("showTeams", null, "-Select Team-", htmlAttributes: new { id = "ddshowTeams", @class = "form-control" })
@Html.DropDownList("showMembers", null, "-Select Team-", htmlAttributes: new {id = "ddshowMembers", @class = "form-control" })
创建一个 jsonResult 函数,GetMembers
和一些魔法:
<script type="text/javascript">
$(document).ready(function () {
//Dropdownlist Selectedchange event
$("#ddshowTeams").change(function () {
console.log("pehla andar");
$("#ddshowMembers").empty();
$.ajax({
type: 'POST',
url: '@Url.Action("GetMembers")',
dataType: 'json',
data: { id: $("#ddshowTeams").val() },
success: function (mems) {
console.log("wich ayaeee");
// states contains the JSON formatted list
// of states passed from the controller
$.each(mems, function (i, member) {
$("#ddshowMembers").append('<option value="'
+ member.Value + '">'
+ member.Text + '</option>');
});
},
error: function (ex) {
alert('Failed to retrieve states.' + ex);
}
});
return false;
})
});
</script>
并在您的控制器中:
public JsonResult GetMembers(int id)
{
return Json(new SelectList(db.Employees.Where(empt => (empt.TeamId == id)), "EmployeeID", "FirstName"));
}
目前我有两个表(团队和员工) 我正在完美地填充 Teams 的下拉列表,接下来我尝试根据员工的 Teams 的 selectedId 填充第二个下拉列表。
控制器:
// GET: CalView
public ActionResult Index(string ses, string DH)
{ //Team Lead Members
var eID = Convert.ToInt32(Session["currentEmployeeID"]);
var EmpID = Session["currentEmpID"];
Employee obj = (from o in db.Employees
where o.EnrollNumber == EmpID
select o).FirstOrDefault();
Department dept = (from dep in db.Departments
where dep.LeadBy == obj.EmployeeId
select dep).FirstOrDefault();
//this works fine
ViewBag.showTeams = new SelectList(db.Teams.Where(tm => (tm.DeptID == dept.DepartmentId) && (dept.LeadBy == eID)), "TeamID","Name");
//this obviously does not
ViewBag.showMembers = new SelectList(db.Employees.Where(empt => (empT.TeamID == selectedIdFromPreviousDropDownList), "EmployeeID", "Employee"));
return View();
}
查看:
if ((Session["UT"] == "DD") && (@ViewBag.DeptLead != null))
{
//this works
@Html.DropDownList("showTeams", null, "-Select Team-", htmlAttributes: new { @class = "form-control" })
//this does not work
@Html.DropDownList("showMembers", null, "-Select Team-", htmlAttributes: new { @class = "form-control" })
}
我需要一些 AJAX 电话吗?或者 POST 方法?对 MVC 完全陌生。
我需要一些 AJAX 电话吗?或者 POST 方法? 好吧,让我们这样做:
给你的 DropdownLists 一些可能的 id:
@Html.DropDownList("showTeams", null, "-Select Team-", htmlAttributes: new { id = "ddshowTeams", @class = "form-control" })
@Html.DropDownList("showMembers", null, "-Select Team-", htmlAttributes: new {id = "ddshowMembers", @class = "form-control" })
创建一个 jsonResult 函数,GetMembers
和一些魔法:
<script type="text/javascript">
$(document).ready(function () {
//Dropdownlist Selectedchange event
$("#ddshowTeams").change(function () {
console.log("pehla andar");
$("#ddshowMembers").empty();
$.ajax({
type: 'POST',
url: '@Url.Action("GetMembers")',
dataType: 'json',
data: { id: $("#ddshowTeams").val() },
success: function (mems) {
console.log("wich ayaeee");
// states contains the JSON formatted list
// of states passed from the controller
$.each(mems, function (i, member) {
$("#ddshowMembers").append('<option value="'
+ member.Value + '">'
+ member.Text + '</option>');
});
},
error: function (ex) {
alert('Failed to retrieve states.' + ex);
}
});
return false;
})
});
</script>
并在您的控制器中:
public JsonResult GetMembers(int id)
{
return Json(new SelectList(db.Employees.Where(empt => (empt.TeamId == id)), "EmployeeID", "FirstName"));
}