Html.DropDownListFor not behaving as expected ASP.net MVC

Posted by rybl on Stack Overflow See other posts from Stack Overflow or by rybl
Published on 2010-06-15T15:51:27Z Indexed on 2010/06/15 15:52 UTC
Read the original article Hit count: 1044

Hello, I am new to ASP.net MVC and I am having trouble getting dropdown lists to work correctly.

I have a strongly typed view that is attempting to use a Html.DropDownListFor as follows:

<%=Html.DropDownListFor(Function(model) model.Arrdep, Model.ArrdepOptions)%>

I am populating the list with a property in my model as follows:

Public ReadOnly Property ArrdepOptions() As List(Of SelectListItem)
    Get
        Dim list As New List(Of SelectListItem)
        Dim arriveListItem As New SelectListItem()
        Dim departListItem As New SelectListItem()
        arriveListItem.Text = "Arrive At"
        arriveListItem.Value = ArriveDepart.Arrive
        departListItem.Text = "Depart At"
        departListItem.Value = ArriveDepart.Depart
        Select Case Me.Arrdep
            Case ArriveDepart.Arrive : arriveListItem.Selected = True
            Case Else : departListItem.Selected = True
        End Select
        list.Add(departListItem)
        list.Add(arriveListItem)
        Return list
    End Get
End Property

The Select Case works find and it sets the right SelectListItem as Selected, but when my view renders the dropdown list no matter what is marked as selected the generated HTML does not have anything selected.

Am I obviously doing something wrong or missing something, but I can't for the life of me figure out what.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about vb.net