c# – ASP.NET MVC中的依赖下拉列表
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – ASP.NET MVC中的依赖下拉列表,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2443字,纯文字阅读大概需要4分钟。
内容图文
![c# – ASP.NET MVC中的依赖下拉列表](/upload/InfoBanner/zyjiaocheng/811/2c0cd4ec881745d6b6a06e3823a51495.jpg)
目前我有两张桌子(团队和员工)
我正在为团队填充dropdownList,接下来我试图填充第二个下拉列表,具体取决于Employses团队的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和一些Magic:
<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"));
}
内容总结
以上是互联网集市为您收集整理的c# – ASP.NET MVC中的依赖下拉列表全部内容,希望文章能够帮你解决c# – ASP.NET MVC中的依赖下拉列表所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。