如何使用 MVC RAZOR 将 DataTable 绑定到 DropDownList?

2024-03-17

我的模型返回数据表的集合,如下所示。

如何使用 MVC RAZOR 将 DataTable 绑定到 DropDownList?

对于每个数据表,我想为其创建一个表行和一个下拉列表。

我尝试了下面的代码:

 foreach (DataTable dataTable in Model.ParameterDataSources)
      {
            <tr>
                <th>
                    Filter:
                </th>
                <td>                    
                    @Html.DropDownList("ddlFilterDataSource", dataTable, "--Select One--") 
                </td>
            </tr>
      }

如何实现这一目标?


一个简单的解决方案是将 DataTable 转换为 DataView,这样就可以了。

    //Controller Code
    ViewBag.Employees = new SelectList(objDt.AsDataView(), "id", "name");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 MVC RAZOR 将 DataTable 绑定到 DropDownList? 的相关文章

随机推荐