This is a migrated thread and some comments may be shown as answers.

Binding a DropDownList to Enums in model data

1 Answer 93 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Ian
Top achievements
Rank 1
Ian asked on 30 Apr 2014, 11:17 AM

I have the following code to bind a DropDownList for the Status property of a User Model. The Status is an enum.

The DropDownList for the first user (row 1 in the table) is rendered correctly - however subsequent rows the DropDownList is not rendered - it is rendered as an empty text box. If I use 'Vanilla Razor' DropDownList then the controls are rendered correctly - what is wrong?

Thanks 

Ian

@model IEnumerable<CLOCS.Models.ApplicationUser>
 
@{
    ViewBag.Title = "Index";
}
 
<h2>Index</h2>
 
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.UserName)
        </th>
        <th>
 
        </th>
    </tr>
 
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.UserName)
                
                 
            </td>
            @*<td>
                @Html.EnumDropDownListFor(modelItem => item.Status)
            </td>*@
            <td>
                @Html.Kendo().DropDownListFor(modelItem => item.Status).BindTo(EnumHelper.GetSelectList(item.Status.GetType()))
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.Id })
            </td>
        </tr>
    }
 
</table>

1 Answer, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 02 May 2014, 06:50 AM
Hi Ian,

This happens because @Html.Kendo().DropDownListFor(modelItem => item.Status) makes all dropdownlist with the same id attribute. You should modify your code a bit in order to have unique ID for the dropdownlists:

@model IList<CLOCS.Models.ApplicationUser>

@for (var i = 0; i < Model.Count; i++)
  {
        <tr>
            <td>
                @Html.DisplayFor(item=> Model[i].UserName)
            </td>
            <td>
                @Html.Kendo().DropDownListFor(item => Model[i].Status).BindTo(EnumHelper.GetSelectList(Model[i].Status.GetType(), Model[i].Status))
            </td>
        </tr>
    }


Regards,
Atanas Korchev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
DropDownList
Asked by
Ian
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Share this question
or