【问题标题】:getJSON to populate dropdown list MVC4getJSON 填充下拉列表 MVC4
【发布时间】:2013-08-07 10:28:07
【问题描述】:

我有以下控制器动作:

[HttpPost]
    public ActionResult GetCourseSections(int courseID)
    {  
         var Sections = dbcontext.CourseSection.Where(cs => cs.CourseID.Equals(courseID)).Select(x => new
        {
            sectionID = x.CourseSectionID,
            sectionTitle = x.Title
        });
        return Json(Sections, JsonRequestBehavior.AllowGet);
    }

这将返回我期望的列表。然后我尝试使用以下代码检索此列表以填充下拉列表:

$.getJSON('@Url.Action("GetCourseSections", "Admin")',
            function(data) {
                var courseSectionDropdown = $('#coursesectiondd');
                courseSectionDropdown.empty();
                courseSectionDropdown.append($('<option/>', {
                    value: 0,
                    text: "Test"
                }));
                $.each(data, function (index, data) {
                    courseSectionDropdown.append($('<option/>', {
                        value: data.value,
                        text: data.text
                    }));
                });
            });

虽然在调试时我能够在添加 JSON 对象时看到它们的列表,但唯一添加的列表项是我正在设置的默认选项“测试”。谁能从我的代码中看到为什么没有读取数据?我在jquery中犯了错误吗

【问题讨论】:

    标签: jquery json asp.net-mvc-4 getjson cascadingdropdown


    【解决方案1】:

    您需要将您的 jQuery 数据属性与方法中的相同名称进行匹配:

    $.getJSON('@Url.Action("GetCourseSections", "Admin")', null,
            function(data) {
                var courseSectionDropdown = $('#coursesectiondd');
                courseSectionDropdown.empty();
                courseSectionDropdown.append($('<option/>', {
                    value: 0,
                    text: "Test"
                }));
                $.each(data, function (index, data) {
                    courseSectionDropdown.append($('<option/>', {
                        value: data.sectionID,
                        text: data.sectionTitle
                    }));
                });
     });
    

    【讨论】:

      【解决方案2】:

      试试这个..它会工作......

        $.getJSON("@Url.Content("~/Admin/GetCourseSections")", null, function (data) {
               $('#coursesectiondd').empty();
               for (i = 0; i < data.length; i++) {    
                       $('#coursesectiondd').append($('<option></option>').text(data[i].sectionTitle).attr('ID', data[i].sectionID));
                } 
          });
      

      【讨论】:

        【解决方案3】:

        它会起作用的......

         function changeSltLeague() {
            $.getJSON('/league/GetByName',
                { leagueName: $("#sltLeagueId option:selected").text() },
                function (data) {
                    currentLeagueId = data.Id;
                    currentLeague = data.Name;
                    showNextRoundInfo('/round/GetNextRoundOfLeague', data.Id);
                });
        }
         $(document).ready(function () {
            var leagueId = @leagueId;
        
            if (leagueId > 0) {
                $('#sltLeagueId option[value=' + leagueId +']').attr("selected", true);
        
                changeSltLeague();
            }
        
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-12
          相关资源
          最近更新 更多