Results 1 to 4 of 4

Thread: DropDownListFor

  1. #1

    Thread Starter
    Registered User
    Join Date
    Sep 2014
    Posts
    3

    DropDownListFor

    Totally lost on this one

    The problem I think I am having is the DropDownListFor, the model.? isn't showing up in the model list
    and the rest I don't know

    Public Function AddRolesToUser() As IEnumerable(Of SelectListItem)

    Using db As New Entities
    Dim roles = db.AspNetRoles

    Dim List As New List(Of SelectListItem)

    For Each r In roles
    List.Add(New SelectListItem With {.Text = r.Name, .Value = r.Id})
    Next
    Return List
    End Using

    Return Nothing
    End Function

    Function AddRole(id As String) As ActionResult
    ViewData("Roles") = helper.AddRolesToUser()

    Return View()
    End Function

    @ModelType IEnumerable(Of ProForma.Web.AspNetRole)

    @Html.DropDownListFor(Function(model) model.Name, ViewData("Roles"))

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: DropDownListFor

    This is your model type: IEnumerable(Of ProForma.Web.AspNetRole). Does that type have a Name property? Of course not. Presumably the ProForma.Web.AspNetRole but that's not what your model is. What you're doing is like expecting to be able to crack the shell of an egg carton. Just because the eggs inside each have a shell doesn't mean that the carton does.

    So, what is this view supposed to display? Is it a single ProForma.Web.AspNetRole object or a list of objects of that type? If it's a list then you need a loop. You use a For Each loop to visit every item in the list and then you get the Name of the current item, just like you might go through the eggs in a carton and crack the shell of each one.

  3. #3

    Thread Starter
    Registered User
    Join Date
    Sep 2014
    Posts
    3

    Re: DropDownListFor

    This is the model with a name property

    Public Class AspNetRolesMetadata
    <Editable(False)>
    Public Property Id As String

    <Display(Name:="Role Name")>
    <Required(ErrorMessage:="Required name of role.")>
    <DisplayFormat(ConvertEmptyStringToNull:=True, NullDisplayText:="[Null]", htmlEncode:=True)>
    <StringLength(50, ErrorMessage:="{0} must be between {2} or {1} characters.", MinimumLength:=4)>
    Public Property Name As String

    End Class

    As posted above @ModelType IEnumerable(Of ProForma.Web.AspNetRole) returns a list of objects

    @ModelType IEnumerable(Of ProForma.Web.AspNetRole)
    @Code
    ViewData("Title") = "AddRole"
    End Code

    <h2>AddRole</h2>

    <p>
    @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table">
    <tr>
    <th>
    @Html.DisplayNameFor(Function(model) model.Name)
    @Html.DropDownListFor(Function(model) model)
    </th>
    <th></th>
    </tr>

    @For Each item In Model
    @<tr>
    <td>
    @Html.DropDownListFor(Function(modelItem) item.Name, New SelectListItem() {New SelectListItem With {.Text = item.Name}}, "Select Role")
    </td>
    </tr>
    Next

    </table>

  4. #4

    Thread Starter
    Registered User
    Join Date
    Sep 2014
    Posts
    3

    Re: DropDownListFor

    What no more farmyard analogous,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width