使用mvc onchange函数不起作用?

时间:2017-10-27 10:58:32

标签: javascript jquery html asp.net-mvc model-view-controller

在编辑注册表单中,国家下拉列表是从数据库加载的,但它没有启用状态下拉列表onchange功能不起作用。

@Html.DropDownList("CountryEdit", null, Model.CountryName, new { @class = "select2-arrow" }) 

State Bind:

<select id="state" class="select2-arrow"></select>

Onchange:State Bind

$(document).ready(function () {
            $("#CountryEdit").on('change', function () {
                var id = $(this).val();
                $("#state").empty();

                $.ajax({
                    type: 'POST',
                    url: '@Url.Action("StateEdit_Bind")',
                    dataType: 'json',
                    data: { CountryID: id },
                    success: function (subcategories) {
                        $.each(subcategories, function (i, subcategory) {
                            $("#state").append('<option value="' + subcategory.Value + '">' + subcategory.Text + '</option>');
                        });
                    },
                    error: function (ex) {
                        alert('Failed to retrieve Sub Categories : ' + ex);
                    }
                });
                return false;
            })
        });

Controller Country Bind:

public void CountryEdit_Bind()
        {
            DataSet ds = dblayer.Get_Country();
            List<SelectListItem> coutrylist = new List<SelectListItem>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                coutrylist.Add(new SelectListItem { Text = dr["CountryName"].ToString(), Value = dr["CountryID"].ToString() });
            }
            ViewBag.CountryEdit = coutrylist;
        }

State Bind:

public JsonResult StateEdit_Bind(string CountryID)
        {
            DataSet ds = dblayer.Get_State(CountryID);
            List<SelectListItem> statelist = new List<SelectListItem>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                statelist.Add(new SelectListItem { Text = dr["StateName"].ToString(), Value = dr["StateID"].ToString() });
            }
            return Json(statelist, JsonRequestBehavior.AllowGet);
        }

1 个答案:

答案 0 :(得分:0)

@Html.DropDownList("CountryEdit", null, Model.CountryName, new { onchange = "GetStates()", @class = "select2-arrow" })

试试这个..

<script  type="text/javascript">

$(document).ready(function () {
});
        function GetStates() {
        var id = $('#CountryEdit').find("option:selected").val();
        $("#state").empty();
             $.ajax({
               type: 'POST',
               url: '@Url.Action("StateEdit_Bind")',
               dataType: 'json',
               data: { CountryID: id },
               success: function (subcategories) {
               $.each(subcategories, function (i, subcategory) {
               $("#state").append('<option value="' + subcategory.Value + '">' + subcategory.Text + '</option>');
                                });
                            },
               error: function (ex) {
                      alert('Failed to retrieve Sub Categories : ' + ex);
                            }
                    });
        }

</script>