开发者

MVC.NET in VB - Select List to Html.Dropdownlist

Seems most of the examples I find are c#, so in some cases I'm left scratching my head... to make a long story short, I'm simply trying to output the selectList of items to a drop-down within my view:

My 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

My Controller:

 Imports S开发者_如何学Goystem
 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

All I'm trying to do now is simply output my "SelectList" within a HTML.DROPDOWNLIST() in my view... any help would be greatly appreciate. When doing a step through, my list items are showing within my "Return view(viewmodel)" watch, but I'm stuck with performing the output.


You will need to add a property in your view model that will hold the selected account type:

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

And then in your view:

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

The drop down list is constructed from the AccType collection of LibAcctType and Id and Text should be properties of this LibAcctType.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜