如何使用 jQuery 将数据绑定到下拉列表(数据来自 LINQ 查询)

本文关键字:数据 LINQ 查询 下拉列表 jQuery 何使用 数据绑定 | 更新日期: 2023-09-27 18:32:10

如何使用jquery将数据绑定到下拉列表(数据来自LINQ查询)? 以下是 HTML 代码

<div class="col">
    <label id="DLbl" class="control-label">Select</label>
    <asp:DropDownList ID="DListDdl" runat="server" ClientIDMode="Static" Class="selectpicker  form-control" data-live-search="true"></asp:DropDownList>
</div>
</div>

和 linq 查询

var DList=(from res in DbContext.transaction
        where res.ProjectId == ProjectId 
       group res by res.Id into res1  
       orderby res1.Key descending
      select new { Id = res1.Key, name = res1.Key.ToString() }).ToList();

如何使用 jQuery 将数据绑定到下拉列表(数据来自 LINQ 查询)

aspx 页面加载中编写此代码。

//Even you can fetch the data object from your business logic layer and pass to the dropdown object datasource.
var DList = (from res in DbContext.transaction
                         where res.ProjectId == ProjectId
                         group res by res.Id into res1
                         orderby res1.Key descending
                         select new { Id = res1.Key, name = res1.Key.ToString()
                         }).ToList();
DListDdl.DataSource = DList;
DListDdl.DataTextField = "name";
DListDdl.DataValueField = "Id";
DListDdl.DataBind();