如何使用jquery将数据绑定到dropdown(数据来自linq查询)?

时间:2016-01-28 05:31:25

标签: c# jquery linq

如何使用jquery将数据绑定到dropdown(数据来自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();

1 个答案:

答案 0 :(得分:0)

将此代码写入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();