c# - 如何使用 Jquery Ajax 调用填充 DropDown?

标签 c# javascript jquery asp.net ajax

我有一个 WebMethod,它获取我想在 DataSet 中填充 DropDown 的数据。 目前我正在使用硬编码对象填充下拉列表。但我想用 webmethod 返回的数据替换这个硬编码对象。

 [System.Web.Services.WebMethod]
         public static string GetDropDownDataWM(string name)
         {
             //return "Hello " + name + Environment.NewLine + "The Current Time is: "
             //    + DateTime.Now.ToString();

             var msg = "arbaaz";

             string[] name1 = new string[1];
             string[] Value = new string[1];
             name1[0] = "@Empcode";
             Value[0] = HttpContext.Current.Session["LoginUser"].ToString().Trim();
             DataSet ds = new DataSet();
             dboperation dbo = new dboperation();
             ds = dbo.executeProcedure("GetDropDownsForVendor", name1, Value, 1);

             return ds.GetXml(); 

         }

客户端(更新 1):

  <script type = "text/javascript">
    function GetDropDownData() {
        var myDropDownList = $('.myDropDownLisTId');

        $.ajax({
            type: "POST",
            url: "test.aspx/GetDropDownDataWM",
            data: '{name: "abc" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                $.each(jQuery.parseJSON(data.d), function () {
                    myDropDownList.append($("<option></option>").val(this['FieldDescription']).html(this['FieldCode']));
                });
            },
            failure: function (response) {
                alert(response.d);
            }
        });
    }
    function OnSuccess(response) {
        console.log(response.d);
        alert(response.d);
    }
</script>

最佳答案

function GetDropDownData() {
    $.ajax({
        type: "POST",
        url: "test.aspx/GetDropDownDataWM",
        data: '{name: "abc" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data.d)
                {
                    $.each(data.d, function (){
                        $(".myDropDownLisTId").append($("<option     />").val(this.KeyName).text(this.ValueName));
                    });
                },
        failure: function () {
            alert("Failed!");
        }
    });
}

关于c# - 如何使用 Jquery Ajax 调用填充 DropDown?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22955839/

相关文章:

c# - 更新在 SalesForce API 中不起作用

javascript - 位掩码位置评估在 JS 中不起作用?

jquery - e.owlCarousel 不是 linux 主机上 yarn encore 生产后的功能

c# - 适当的类似列表的排序数据结构

c# - 删除与 IDictionary 中的条件匹配的所有项目

javascript - Turbolinks Rails 5 切换事件不起作用

javascript - 制作一个从屏幕右侧弹出的菜单

iframe 内的 JavaScript mousemove 事件

javascript - 如何捕获div内容并在模态框中显示?

c# - 有没有办法在.NET Core FilterAttribute 中获取请求正文?