【问题标题】:asp.net web form dropdownlist from web api来自 web api 的 asp.net web 表单下拉列表
【发布时间】:2018-08-08 17:10:42
【问题描述】:

需要一些帮助,了解如何使用从 web api (JSON) 获取的数据填充下拉列表。 尝试了多篇文章,但不知何故它不起作用。

我从 api 得到的数据: 链接:http://192.168.0.11/api/lookup

["Daniel","Mark","John"]

default.aspx 站点:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <script src="Scripts/jquery-1.10.2.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                var userrs = $('#userrs');

                $('#btnRefresh').click(function () {
                    $.ajax({
                        type: 'GET',
                        url: 'http://192.168.0.11/api/lookup',
                        dataType: 'json',
                        success: function (data) {
                            userrs.empty();
                            $.each(data, function (val) {
                                var userid = val
                                $("#DropDownList1").append('<option>' + userid + '</option >')

                            });
                        }
                    });
                });
            });
</script>
<body>

    <form id="form1" runat="server" class="auto-style1">
        <asp:Label ID="Label1" runat="server" Text="Select User" CssClass="table"></asp:Label>


        <br/>
        <asp:DropDownList ID="DropDownList1" runat="server" Height="17px" Width="158px">
        </asp:DropDownList>
        <asp:Button ID="btnRefresh" runat="server" OnClick="btnRefresh_Click1" Text="Refresh" />
        <ul id= "userrs"></ul>
        <br />
        <asp:Button ID="btnSend" runat="server" OnClick="btnSend_Click1" Text="Send" />
        <br />
        <asp:TextBox ID="txtbox" runat="server" Height="104px" OnTextChanged="txtbox_TextChanged" Width="272px"></asp:TextBox>
        <br />
        </form>
</body>
</html>

目标是通过点击按钮 btnRefresh 将他从 api 获得的 3 个名称填充到 DropDownList1 中。

由于某种原因,它没有在下拉列表中添加任何内容。

提前致谢。

【问题讨论】:

    标签: c# jquery asp.net-web-api asp.net-ajax


    【解决方案1】:

    你需要一个索引。 (并添加分号。)

    $.each(data, function (idx, val) {
        var userid = val;
        $("#DropDownList1").append('<option>' + userid + '</option >');    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多