MVC 4 Jquery $ .post问题,发布不工作:(

时间:2013-02-26 17:43:01

标签: javascript jquery asp.net-mvc-3 asp.net-mvc-4

在下文中,我的initData是ID为#s的国家/地区名称。

我想要做的是更改国家/地区,获取我在课堂上实施的城市名称列表并且它有效。然而,在下拉变化时,我不知道如何回发并获得这些城市:(

任何帮助都将不胜感激。

由于

    <select class="Countries"></select>


    <script type="text/javascript">
    $(document).ready(function() {
        var initData= @Html.Raw(Json.Encode(Model))

        $(function() {
            var $select = $('.Countries');
            $(".Countries").append("<option value='0'>--Select Country--</option>");
            $.each(initialData, function(i, country) {
                var option = $('<option>', {
                    value: country.CountryID
                }).html(country.CountryName).appendTo($select);

            });
        });

        $('.Countries').change(function() {
             var postData = [];
                alert(postData);
                postData.push({value: $(this).val()});
                $.post('@Url.Action("Cities")', postData, function (data) {
                    if (data.success) {
                        alert('Retrieve list of cities');
                    }

                }, 'json');

        });
    });
    </script>

我的控制器:

     public ActionResult Cities(int id)
    {
        List<CityModel> listCity = _da.GetCities(id);
        return RedirectToAction("Index");
    }

1 个答案:

答案 0 :(得分:0)

我的ID未定义,表示没有相关名称。和postdata只包含Value .. ..添加ID后它起作用。

  postData.push({name: 'id', value: $(this).val()});