Question

combo loads ok, but item "3" does not get selected. How can I select this item?

Here's the code I'm using for the list and selection.

@Code

    Dim Items As New Collections.Generic.List(Of SelectListItem)
    Items.Add(New SelectListItem With {.Value = "1", .Text = "Usuario registrado"})
    Items.Add(New SelectListItem With {.Value = "2", .Text = "Administrador"})
    Items.Add(New SelectListItem With {.Value = "3", .Text = "Supervisor"})
    Items.Add(New SelectListItem With {.Value = "4", .Text = "Usuario de pulmón"})
    Items.Add(New SelectListItem With {.Value = "5", .Text = "Usuario de picking"})

End Code


@Html.LabelFor(Function(m) m.Profile)
@Html.DropDownListFor(Function(m) m.Profile, New SelectList(Items, "Value", "Text", "3"))

Controller. In this section I initialize the profile property.

    Public Function Edit() As ActionResult
    Dim Model As New EditModel

    Dim User = Membership.FindUsersByName(Request("UserName"))(Request("UserName"))
    Model.UserName = User.UserName

    If Roles.IsUserInRole(User.UserName, Models.AppRoles.Administrador.ToString) Then
        Model.Profile = Models.AppRoles.Administrador

    ElseIf Roles.IsUserInRole(User.UserName, Models.AppRoles.Supervisor.ToString) Then
        Model.Profile = Models.AppRoles.Supervisor

    ElseIf Roles.IsUserInRole(User.UserName, Models.AppRoles.UsuarioPulmon.ToString) Then
        Model.Profile = Models.AppRoles.UsuarioPulmon

    ElseIf Roles.IsUserInRole(User.UserName, Models.AppRoles.UsuarioPicking.ToString) Then
        Model.Profile = Models.AppRoles.UsuarioPicking

    ElseIf Roles.IsUserInRole(User.UserName, Models.AppRoles.UsuarioRegistrado.ToString) Then
        Model.Profile = Models.AppRoles.UsuarioRegistrado

    End If

    Model.Email = User.Email

    Return View(Model)
End Function



Public Class EditModel

<Required()>
<Display(Name:="Perfil")>
Public Property Profile() As Models.AppRoles

<Required()> _
<Display(Name:="Nombre de usuario")> _
Public Property UserName() As String

<Required()> _
<DataType(DataType.EmailAddress)> _
<Display(Name:="E-mail")> _
Public Property Email() As String

End Class

Namespace Models
Public Enum AppRoles
    UsuarioRegistrado = 1
    Administrador = 2
    Supervisor = 3
    UsuarioPulmon = 4
    UsuarioPicking = 5
End Enum

End Namespace

Was it helpful?

Solution

You should initialize Profile property with the value you want to be selected in the list. If Profile is not an int, create a property SelectedProfileId and use this property

@Html.DropDownListFor(Function(m) m.SelectedProfileId, New SelectList(Items, "Value", "Text"))

OTHER TIPS

Try this, I bind it to Model's property, and it works.

@Html.DropDownListFor(Function(m) m.Profile, New SelectList(Items, "Value", "Text", Model.Profile))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top