使用mvc中的查询绑定下拉列表

时间:2011-07-25 12:50:31

标签: jquery asp.net-mvc

我想使用jquery绑定drop dawn list依赖于另一个ddl我正在使用jvc和mvc,这里是我正在使用的代码:

 public IList<Categories> GetProductCategories(int ProductID, int Inst)
        {
           //items is a list get it some way no need for details 


                return items;
            }
        }

这里是jquery代码:

$(document).ready(function () {

    $('#ddlProductType').change(function () {
        var x = $('#ddlInstallation').val();


        $.ajax({
            type: 'POST',
            url: '/Home/GetProductCategories',
            data: { ProductID: $(this).val(), Inst: x },
            success: function (data) {
                var markup = '';
                for (var x = 0; x < data.length; x++) {
                    markup += "<option value='" + data[x].Value + "'>" + data[x].Text + "</option>";
                }
                $('#ddlCategory').html(markup).show();
            }
        });
    });
)};

任何帮助:)

1 个答案:

答案 0 :(得分:2)

我找到了解决方案首先我需要的是不要将它作为一个函数使用,你可以执行以下操作:

  public JsonResult GetProductCategories(int ProductID, int Inst)
        {
                //items is your list what ever how you get it 
                JsonResult result = new JsonResult();
                result.Data = items;
                return result;
            }

并且jquery代码中没有错误只需尝试