VB中的MVC.NET - 选择列表到Html.Dropdownlist

时间:2010-07-20 13:20:01

标签: asp.net-mvc vb.net asp.net-mvc-2

似乎我发现的大多数例子都是c#,所以在某些情况下我只是在摸不着头......长话短说,我只是想把项目的selectList输出到下拉列表中在我看来:

我的ViewModel:

Imports System.Web
Imports Whitebox.UI


Namespace ViewModels
Public Class TFS_VModel
    Public Property AccType() As IEnumerable(Of LibAcctType)
        Get
            Return m_types
        End Get

        Set(ByVal value As IEnumerable(Of LibAcctType))
            m_types = value
        End Set
    End Property

    Private m_types As IEnumerable(Of LibAcctType)
End Class
End Namespace

我的控制器:

 Imports System
 Imports System.Collections.Generic
 Imports System.Linq
 Imports System.Web.Mvc
 Imports Whitebox.UI
 Imports Whitebox.UI.ViewModels

 <HandleError()> _
 Public Class TFSController
     Inherits Controller

     Dim _DB As New BlackBoxNormalizedEntities()

     Function TFSMain() As ActionResult
         Dim AccTypeList = (From m In _DB.LibAcctType Select m).ToList()

         Dim viewModel As New TFS_VModel()
         viewModel.AccType = AccTypeList

         Return View(viewModel)
     End Function


End Class

我现在要做的就是在我的视图中输出HTML.DROPDOWNLIST()中的“SelectList”...任何帮助都会非常感激。一步一步,我的列表项目显示在我的“返回视图(viewmodel)”监视器中,但我仍然坚持执行输出。

1 个答案:

答案 0 :(得分:1)

您需要在视图模型中添加一个属性,该属性将保留所选的帐户类型:

Public Class TFS_VModel
    Public Property AccType() As IEnumerable(Of LibAcctType)
        Get
            Return m_types
        End Get

        Set(ByVal value As IEnumerable(Of LibAcctType))
            m_types = value
        End Set
    End Property

    Private m_selectedAccType As String
    Public Property SelectedAccType() As String
        Get
            Return m_selectedAccType
        End Get
        Set(ByVal value As String)
            m_selectedAccType = value
        End Set
    End Property

    Private m_types As IEnumerable(Of LibAcctType)
End Class

然后在你看来:

<%= Html.DropDownListFor(Function(x) x.SelectedAccType, New SelectList(Model.AccType, "Id", "Text", Model.SelectedAccType)) %>

下拉列表是根据AccTypeLibAcctType的{​​{1}}集合构建的,而Id应该是此Text的属性。