2

我是 ASP.net MVC 的新手,我无法让下拉列表正常工作。

我有一个试图使用 Html.DropDownListFor 的强类型视图,如下所示:

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

我正在使用模型中的属性填充列表,如下所示:

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

Select Case 可以找到并将正确的 SelectListItem 设置为 Selected,但是当我的视图呈现下拉列表时,无论标记为已选择的内容,生成的 HTML 都没有选择任何内容。

我显然做错了什么或错过了什么,但我无法终生弄清楚是什么。

4

3 回答 3

2

事实证明我必须编写一个下拉列表助手。我在这里找到了这篇文章。这是我的代码,以防其他人需要它。它是从文章中的 C# 示例大致翻译成 VB 的。

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
于 2010-06-25T01:45:36.577 回答
1

尝试这个:

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
于 2010-06-16T20:14:59.020 回答
0

SelectList 构造函数的第 4 个参数类型是 Object,但它可能需要与列表中的对象类型相同的对象(第 1 个参数)。因此,正确的语法应该是这样的:

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
于 2010-06-19T17:16:40.503 回答