Domanda

Sto cercando di associare un elenco a discesa in ASP.NET MVC a una classe. Fortunatamente per me questo sarà un numero tra 1 e 10, tuttavia non sto ottenendo l'output nel mio HTML che mi sarei aspettato. Ho impostato sia il valore che le proprietà del testo che sono popolate nella Vista, ma il valore selezionato si rifiuta di renderizzare. Questo è all'interno del metodo Edit iniziale del mio controller. Qualche idea?

    Dim SC As SuperCategory = Model.Fast.Pull(DB.SuperCategory, ID)

    Dim list As New List(Of SelectListItem)
    list.Add(New SelectListItem With {.Value = 1, .Text = "1", .Selected = (.Value = SC.Sort)})
    list.Add(New SelectListItem With {.Value = 2, .Text = "2", .Selected = (.Value = SC.Sort)})
    list.Add(New SelectListItem With {.Value = 3, .Text = "3", .Selected = (.Value = SC.Sort)})
    list.Add(New SelectListItem With {.Value = 4, .Text = "4", .Selected = (.Value = SC.Sort)})
    list.Add(New SelectListItem With {.Value = 5, .Text = "5", .Selected = (.Value = SC.Sort)})
    list.Add(New SelectListItem With {.Value = 6, .Text = "6", .Selected = True})
    list.Add(New SelectListItem With {.Value = 7, .Text = "7", .Selected = (.Value = SC.Sort)})
    list.Add(New SelectListItem With {.Value = 8, .Text = "8", .Selected = (.Value = SC.Sort)})
    list.Add(New SelectListItem With {.Value = 9, .Text = "9", .Selected = (.Value = SC.Sort)})
    list.Add(New SelectListItem With {.Value = 10, .Text = "10", .Selected = (.Value = SC.Sort)})

    ViewData("Sort") = list

L'HTML nella vista è simile al seguente:

Ordina:        <% = Html.DropDownList (" Sort " ;, CType (ViewData (" Sort "), IEnumerable (Of SelectListItem)))% gt;

È stato utile?

Soluzione

Se ViewData["Sort"] implementa IEnumerable<SelectListItem> non è necessario il secondo parametro. Hai provato questo ?:

<%= Html.DropDownList("Sort") %>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top