当另一个选择列表发生更改时,Ajax调用填充选择列表

时间:2016-04-04 01:38:49

标签: jquery json ajax asp.net-mvc

this post之后,当另一个“国家/地区”选择列表发生变化时,我正在尝试填充“城市”的选择列表。我采用了与链接帖子相同的方法,并按照下面的概述实现了Darin对成功的建议()。它“大部分时间”工作得很好(这真的很奇怪)。我的意思是,在我的所有'国家'中,90%的更改事件返回选择列表项目对象,另外10%返回单个字符串值。为什么会有所不同?它应该工作与否......

查看

<!--Country-->
                    <div class="form-group col-md-3">
                        <label class="control-label ">City </label>
                        <br />
                       <select id="Country" name="Country" class="form-control">
                           <option value="">List of countries from Model...</option>
                       </select>
                    </div>


<!--City-->
                    <div class="form-group col-md-3">
                        <label class="control-label ">City </label>
                        <br />
                       <select id="City" name="City" class="form-control">
                           <option value="">Select a country first...</option>
                       </select>
                    </div>


<!--Inside my script -->
<script>
 $('#Country').change(function () {
        var selectedValue = $(this).val();
        $.ajax({
            url: '@Url.Action("CityDropDownList", "Home")',
            type: "GET",
            data: { country: selectedValue },
            success: function (result) {

                var cityDropDownList = $('#City');
                cityDropDownList.empty();
                $.each(result, function () {
                    cityDropDownList.append(
                        $('<option/>', {
                            value: this.Value,
                            text: this.Text
                        })
                    );
                });
            }
        });
    });
</script>

控制器

 public JsonResult CityDropDownList(string country)
    {
        var results = (from c in db.PageStats
                       where c.Country.Equals(country)
                       orderby c.City
                       select c.City).ToList().Distinct();

            //db.PageStats.Where(x => x.Country.Equals(country)).OrderBy(x => x.City).Select(x => x.City).Distinct().ToList();

        List<SelectListItem> cities = new List<SelectListItem>();

        foreach(var item in results)
        {
            SelectListItem city = new SelectListItem
            {
                Text = item,
                Value = item
            };
            cities.Add(city);
        }             
        return Json(cityList, JsonRequestBehavior.AllowGet);
    }

1 个答案:

答案 0 :(得分:3)

解决

$('#Country').change(function () {
    var selectedValue = $(this).val();
    $.get({
        url: '@Url.Action("CityDropDownList", "Home")',
        data: { country: selectedValue },

        success: function (result) {

            alert(result);
            var cities = $('#City');
            cities.empty();

            $.each(result, function () {

                cities.append(
                        $('<option>', {
                            value: this.Value
                        }).text(this.Text)
                    );
            });
        }
    });
});