首页 文章

将简单的排序下拉列表绑定到MVC中的列表

提问于
浏览
1

我试图将ASP.NET MVC中的下拉列表绑定到一个类 . 幸运的是,这将是1到10之间的数字,但是我没有得到我期望的HTML输出 . 我已经设置了在视图中填充的值和文本属性,但是所选值拒绝渲染 . 这是在我的控制器的初始Edit方法中 . 有任何想法吗?

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

视图中的HTML如下所示:

排序:<%= Html.DropDownList(“排序”,CType(ViewData(“排序”),IEnumerable(Of SelectListItem)))%>

1 回答

  • 0

    如果 ViewData["Sort"] 实现 IEnumerable<SelectListItem> ,则不需要第二个参数 . 你试过这个吗?:

    <%= Html.DropDownList("Sort") %>
    

相关问题