Saba
Saba

Reputation: 71

How to bind Dropdownlist using Datatable In Mvc

For each DataTable, I'd like to create a table row and a drop down list for it. My code here,

@model System.Data.DataTable
....
<table>
    <thead>
        <tr>
            @foreach (DataColumn col in Model.Columns)
            {
                <th>@col.ColumnName</th>
            }
        </tr>
    </thead>
    <tbody>
        @foreach (DataRow row in Model.Rows)
        {
            <tr>
                @foreach (DataColumn col in Model.Columns)
                {
                    <td>
                        @Html.DropDownListFor();
                    </td>
                }
            </tr>
        }
    </tbody>
</table>

Upvotes: 0

Views: 1188

Answers (1)

Saba
Saba

Reputation: 71

   @{
        List<SelectListItem> li = new List<SelectListItem>();
        foreach (DataRow row in Model.Rows)
        {
            li.Add(new SelectListItem() { Text = row["associatename"].ToString(), Value = row["associateid"].ToString() });
        }
    }
    @Html.DropDownList("test", li)

Upvotes: 1

Related Questions