所以我需要为Web应用程序实现一些下拉列表。我使用实体模型将我的Web应用程序链接到我的数据库,创建了所有类和所有内容。如何创建一个下拉列表,其中填充了我的数据库中的表中的列的项目?
以下是我尝试使用的其中一个模型的类:
Partial Public Class gasrevperyear
Public Property ID As Integer
Public Property year As Integer
Public Property amount As Integer
End Class
以下是我要在
上实现下拉菜单的视图中的代码@ModelType IEnumerable(Of gasrevperyear)
@Code
ViewData("Title") = "Gasoline Revenue Per Year"
End Code
<h2>Gasoline Revenue Per Year</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(Function(model) model.year)
</th>
<th>
@Html.DisplayNameFor(Function(model) model.amount)
</th>
<th></th>
</tr>
@For Each item In Model
@<tr>
<td>
@Html.DisplayFor(Function(modelItem) item.year)
</td>
<td>
@Html.DisplayFor(Function(modelItem) item.amount)
</td>
<td>
@Html.ActionLink("Edit", "Edit", New With {.id = item.ID }) |
@Html.ActionLink("Details", "Details", New With {.id = item.ID }) |
@Html.ActionLink("Delete", "Delete", New With {.id = item.ID })
</td>
</tr>
Next
</table>
@Html.DropDownListFor(Model.gasrevperyear, gasrevperyear.year, "1990")
感谢您的帮助
答案 0 :(得分:0)
首先,您需要创建下拉列表功能。我通常使用LINQ查询执行此操作,但您也可以手动执行此操作:
使用查询:
Public Function gasrevperyearDD() As IEnumerable(Of SelectListItem)
Dim List As New List(Of SelectListItem)
List.Add(New SelectListItem With {.Text = "Select An Item", .Value = 0})
Dim valQ = From v In table
Where v IsNot Nothing
Select v
For Each i In valQ
List.Add(New SelectListItem With {.Text = i.SomeString, .Value = i.ID})
Next
gasrevperyearDD = List
End Function
无查询
Public Function gasrevperyearDD() As IEnumerable(Of SelectListItem)
Dim List As New List(Of SelectListItem)
List.Add(New SelectListItem With {.Text = "Select An Item", .Value = 0})
List.Add(New SelectListItem With {.Text = "SomeString", .Value = 1})
List.Add(New SelectListItem With {.Text = "SomeString", .Value = 2})
gasrevperyearDD = List
End Function
然后在定义视图的控制器中,您需要创建一个调用函数ViewData("DropDown") = gasrevperyearDD
最后在您的视图中,您可以简单地调用ViewData元素来填充下拉列表:
@Html.DropDownListFor(Function(m) m.gasrevperyear, DirectCast(ViewData("gasrevperyearDD"), IEnumerable(Of SelectListItem)))
或
@Html.DropDownListFor(Function(m) m.gasrevperyear, ViewData("gasrevperyearDD"))