Binding a DropDownList to Enums in model data

2 posts, 0 answers
  1. Ian
    Ian avatar
    9 posts
    Member since:
    Nov 2013

    Posted 30 Apr 2014 Link to this post

    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>
  2. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 02 May 2014 Link to this post

    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.

     
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top