Ajax联动动态为@Html.DropDownListFor赋值

前端之家收集整理的这篇文章主要介绍了Ajax联动动态为@Html.DropDownListFor赋值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<script type="text/javascript">
 function getTeams(deptCode) {
 $.ajax({
 url: "@Url.Action("GetTeams","User")",data: { departmentCode: deptCode },dataType: "json",type: "POST",error: function () {
 alert("An error occurred.");
 },success: function (data) {
 var items = "";
 $.each(data,function (i,item) {
 items += "<option value=\"" + item.Value + "\">" + item.Text + "</option>";
 });

 $("#@Html.IdFor(m => m.TeamId)").html(items);
 }
 });
 }

 $(document).ready(function () {
 $("#@Html.IdFor(m => m.DepartmentCode)").change(function () {
 var deptCode = $("#@Html.IdFor(m => m.DepartmentCode)").val();

 getTeams(deptCode);
 });
 });
 </script>


根据部门ID(deptCode)获取该部门下的组(Team)。

Controller:

User的GetTeams(string departmentCode)返回一个集合,通过

$.each(data,item) 
进行循环添加

猜你在找的Ajax相关文章