是否可以从webmethod返回list并传递返回值来使用jquery绑定html选择?

时间:2022-12-05 17:07:11

Im trying to bind an html select and i wrote a webmethod for that, which returns a list. How can i use this return value to bind my select control using jquery.... ? I'm stuck... The code is appended herewith :

我尝试绑定一个html选择,我为它写了一个webmethod,它返回一个列表。如何使用这个返回值绑定我的选择控制使用jquery ....吗?我卡住了……本守则附于下文:

function columnDropdownScript() {
  var reqTableNameParameter = "Designation";  //$('#ddlTableNames').text;
  var requestTableParameters = '{' +
                        'selTableName:"' + reqTableNameParameter + '"}';
  // Configure AJAX call to server                        
  $.ajax({
    type: "POST",
    url: "Webtop.aspx/FillColumnDropdown",
    data: requestTableParameters,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: DisplayColumnNames, //Event that'll be fired on Success
    error: DisplayError //Event that'll be fired on Error
  });

}
function DisplayColumnNames(serverResponse) {

  $("#ddlColumnNames").get(0).options.length = 0;
  $("#ddlColumnNames").get(0).options[0] = new Option("Select", "-1");
  $.each(serverResponse.d, function(index, item) {
    $("#ddlColumnNames").get(0).options[$("#ddlColumnNames").get(0).options.length] = new Option(item.Display, item.Value);
  });
  alert('Check Column DropDown');
}




 [WebMethod]
    public static List<string> FillColumnDropdown(string selTableName)
    {
        int x=1;
        string selectedTable = selTableName;
        List<string> columnsToBind = new List<string>();
        foreach (Columns column in Metadata.columnsOfSelectedTables)
        {
            if (column.TableName.Equals(selectedTable))
            {
                columnsToBind.Add(column.ColumnName);
            }
        }
       return columnsToBind;
    }

2 个解决方案

#1


2  

// I haven't tested this, but off the top of my head, this should do the trick // (note, this is appending to the list. you may need to clear if called multiple times)

//我还没有测试过这个,但是在我的头顶上,这应该是一个技巧//(注意,这是在列表的后面。如果多次调用,您可能需要清除)

$.ajax({
    type: "POST",
    url: "Webtop.aspx/FillColumnDropdown",
    data: requestTableParameters,
    //contentType: "plain/text",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        for (var i = 0, l = msg.length; i < l; i++) {
            $("#the_selectbox").append("<option>" + msg.d[i] + "</option>");
        }
    },
    error: DisplayError //Event that'll be fired on Error
  });

#2


0  

Just for curiosity, why did you do a web method for that instead of adding it onload / postback trigger?

出于好奇,为什么要为它做一个web方法而不是添加onload / postback触发器?

And where are you casting your list to json in code behind? Wouldn't it's return type be xml?

在后面的代码中,你会把你的列表放到json中吗?它的返回类型不是xml吗?

Anyway, jQuery objects properties ask for anonymous functions:

jQuery对象属性要求匿名函数:

$.ajax({
    // ...
    success:function(serverResponse){
        //success code
    },
    error:function(serverResponse){
        //error code
    },
});

#1


2  

// I haven't tested this, but off the top of my head, this should do the trick // (note, this is appending to the list. you may need to clear if called multiple times)

//我还没有测试过这个,但是在我的头顶上,这应该是一个技巧//(注意,这是在列表的后面。如果多次调用,您可能需要清除)

$.ajax({
    type: "POST",
    url: "Webtop.aspx/FillColumnDropdown",
    data: requestTableParameters,
    //contentType: "plain/text",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        for (var i = 0, l = msg.length; i < l; i++) {
            $("#the_selectbox").append("<option>" + msg.d[i] + "</option>");
        }
    },
    error: DisplayError //Event that'll be fired on Error
  });

#2


0  

Just for curiosity, why did you do a web method for that instead of adding it onload / postback trigger?

出于好奇,为什么要为它做一个web方法而不是添加onload / postback触发器?

And where are you casting your list to json in code behind? Wouldn't it's return type be xml?

在后面的代码中,你会把你的列表放到json中吗?它的返回类型不是xml吗?

Anyway, jQuery objects properties ask for anonymous functions:

jQuery对象属性要求匿名函数:

$.ajax({
    // ...
    success:function(serverResponse){
        //success code
    },
    error:function(serverResponse){
        //error code
    },
});