0

组合加载正常,但未选择项目“3”。我怎样才能选择这个项目?

这是我用于列表和选择的代码。

@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"))

控制器。在本节中,我将初始化配置文件属性。

    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

结束类

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

结束命名空间

4

2 回答 2

0

Profile您应该使用要在列表中选择的值来初始化属性。如果Profile不是int,则创建一个属性SelectedProfileId并使用此属性

@Html.DropDownListFor(Function(m) m.SelectedProfileId, New SelectList(Items, "Value", "Text"))
于 2013-01-17T02:31:29.373 回答
0

试试这个,我将它绑定到模型的属性上,它可以工作。

@Html.DropDownListFor(Function(m) m.Profile, New SelectList(Items, "Value", "Text", Model.Profile))
于 2013-01-17T04:13:26.590 回答