English 中文(简体)
Html.DropDownListFor not behaving as expected ASP.net MVC
原标题:

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.

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

最佳回答

It turns out I had to write a dropdown list helper. I found the article here. Here is my code in case someone else needs it. It is roughly translated to VB from the C# example in the article.

Imports System.Linq.Expressions
Imports System.Runtime.CompilerServices

Namespace Helpers

    Public Module HtmlDropDownExtensions

        <Extension()> _
        Public Function EnumDropDownList(Of TEnum)(ByVal htmlHelper As HtmlHelper, ByVal name As String, ByVal selectedValue As TEnum) As MvcHtmlString

            Dim values As IEnumerable(Of TEnum) = [Enum].GetValues(GetType(TEnum))

            Dim list As New List(Of SelectListItem)
            For Each value As TEnum In values
                Dim selectListItem As New SelectListItem()
                selectListItem.Text = value.ToString()
                selectListItem.Value = value.ToString()
                selectListItem.Selected = (value.Equals(selectedValue))
                list.Add(selectListItem)
            Next
            Dim items As IEnumerable(Of SelectListItem) = list

            Return htmlHelper.DropDownList(name, items)

        End Function

        <Extension()> _
        Public Function EnumDropDownListFor(Of TModel, TEnum)(ByVal htmlHelper As HtmlHelper(Of TModel), ByVal expression As Expression(Of Func(Of TModel, TEnum))) As MvcHtmlString

            Dim metadata As ModelMetadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData)

            Dim values As IEnumerable(Of TEnum) = [Enum].GetValues(GetType(TEnum))

            Dim list As New List(Of SelectListItem)
            For Each value As TEnum In values
                Dim selectListItem As New SelectListItem()
                selectListItem.Text = value.ToString()
                selectListItem.Value = value.ToString()
                selectListItem.Selected = (value.Equals(metadata.Model))
                list.Add(selectListItem)
            Next
            Dim items As IEnumerable(Of SelectListItem) = list

            Return htmlHelper.DropDownListFor(expression, items)

        End Function

    End Module

End Namespace
问题回答

Try this:

Public ReadOnly Property ArrdepOptions() As SelectList
    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
        list.Add(departListItem)
        list.Add(arriveListItem)
        Return New SelectList(list, Me.Arrdep)
    End Get
End Property

The SelectList constructor s 4-th parameter type is Object, but it probably wants an object of the same type as the objects in the list (1-st param). Therefore, the correct syntax should be this:

Public ReadOnly Property ArrdepOptions() As SelectList
  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
    list.Add(departListItem)
    list.Add(arriveListItem)

    Select Case Me.Arrdep
        Case ArriveDepart.Arrive : Return New SelectList(list, "Text", "Value", arriveListItem)
        Case Else : Return New SelectList(list, "Text", "Value", departListItem)
    End Select

  End Get
End Property




相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签