views:

43

answers:

1

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 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

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.

+1  A: 

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.

Darin Dimitrov
Thanks! I managed to render the drop-down by creating a partial view through my controller using <%: Html.DropDownList("Acctype", New SelectList(Model.AccType, "AcctType", "Description"))%>, which appears to work and source is showing what I need it to in terms of values.... Now I need to figure out how to allow for multiple selects within my drop-down!
denisb
You could use `MultiSelectList` instead of `SelectList` which works with the `Html.ListBox` helper.
Darin Dimitrov
PERFECT! Thanks! Works!
denisb