views:

393

answers:

1

Hi guys,

Im trying to bind a dropdownlist in ASP.NET MVC to a class. Luckily for me this will be a number between 1 and 10, however I'm not getting the output in my HTML that I would expect. I've set both the value and text properties which are populated ok in the View, but the selected value refuses to render. This is within the initial Edit method of my controller. Any ideas?

    Dim SC As SuperCategory = Model.Fast.Pull(DB.SuperCategory, ID)

    Dim list As New List(Of SelectListItem)
    list.Add(New SelectListItem With {.Value = 1, .Text = "1", .Selected = (.Value = SC.Sort)})
    list.Add(New SelectListItem With {.Value = 2, .Text = "2", .Selected = (.Value = SC.Sort)})
    list.Add(New SelectListItem With {.Value = 3, .Text = "3", .Selected = (.Value = SC.Sort)})
    list.Add(New SelectListItem With {.Value = 4, .Text = "4", .Selected = (.Value = SC.Sort)})
    list.Add(New SelectListItem With {.Value = 5, .Text = "5", .Selected = (.Value = SC.Sort)})
    list.Add(New SelectListItem With {.Value = 6, .Text = "6", .Selected = True})
    list.Add(New SelectListItem With {.Value = 7, .Text = "7", .Selected = (.Value = SC.Sort)})
    list.Add(New SelectListItem With {.Value = 8, .Text = "8", .Selected = (.Value = SC.Sort)})
    list.Add(New SelectListItem With {.Value = 9, .Text = "9", .Selected = (.Value = SC.Sort)})
    list.Add(New SelectListItem With {.Value = 10, .Text = "10", .Selected = (.Value = SC.Sort)})

    ViewData("Sort") = list

HTML in the view looks like this:

Sort: <%=Html.DropDownList("Sort", CType(ViewData("Sort"), IEnumerable(Of SelectListItem)))%>

A: 

If ViewData["Sort"] implements IEnumerable<SelectListItem> there is no need in second parameter. Did you try this?:

<%= Html.DropDownList("Sort") %>
eu-ge-ne