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

发布于 2024-07-25 03:13:15 字数 1409 浏览 3 评论 0原文

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

    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)))%>

Im trying to bind a dropdownlist in ASP.NET MVC to a class. Luckily for me this will be a number between 1 and 10, however I'm not getting the output in my HTML that I would expect. I've set both the value and text properties which are populated ok in the View, but the selected value refuses to render. This is within the initial Edit method of my controller. Any ideas?

    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 in the view looks like this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰葑 2024-08-01 03:13:15

如果 ViewData["Sort"] 实现 IEnumerable 则不需要第二个参数。 你尝试过这个吗?:

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

If ViewData["Sort"] implements IEnumerable<SelectListItem> there is no need in second parameter. Did you try this?:

<%= Html.DropDownList("Sort") %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文