Html.DropDownListFor not behaving as expected ASP.net MVC

╄→尐↘猪︶ㄣ 提交于 2019-12-22 00:19:12

问题


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.


回答1:


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



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/3046811/html-dropdownlistfor-not-behaving-as-expected-asp-net-mvc

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