Html.DropDownListFor not behaving as expected ASP.net MVC

亡梦爱人 提交于 2019-12-04 16:58:41

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!