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

前端之家收集整理的这篇文章主要介绍了Ajax联动动态为@Html.DropDownListFor赋值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. <script type="text/javascript">
  2. function getTeams(deptCode) {
  3. $.ajax({
  4. url: "@Url.Action("GetTeams","User")",data: { departmentCode: deptCode },dataType: "json",type: "POST",error: function () {
  5. alert("An error occurred.");
  6. },success: function (data) {
  7. var items = "";
  8. $.each(data,function (i,item) {
  9. items += "<option value=\"" + item.Value + "\">" + item.Text + "</option>";
  10. });
  11.  
  12. $("#@Html.IdFor(m => m.TeamId)").html(items);
  13. }
  14. });
  15. }
  16.  
  17. $(document).ready(function () {
  18. $("#@Html.IdFor(m => m.DepartmentCode)").change(function () {
  19. var deptCode = $("#@Html.IdFor(m => m.DepartmentCode)").val();
  20.  
  21. getTeams(deptCode);
  22. });
  23. });
  24. </script>


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

Controller:

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

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

猜你在找的Ajax相关文章