我遇到了使用SelectList和Html.DropDownList的问题,我一直无法通过查看以前问题的答案来解决问题。如何正确使用Html.DropDownList与SelectList?
我有一个查看下面的代码:
Dim myItems As New Collection
Dim thisItem As SelectListItem
For Each responsibility In Model.Responsibilities.EBS_Resonsibility
thisItem = New SelectListItem
thisItem.Text = responsibility.RESPONSIBILITY_NAME
thisItem.Value = responsibility.RESPONSIBILITY_ID
myItems.Add(thisItem)
Next
从我的模型考虑每个项目,把它变成一个SelectListItem,并将其添加到集合。
然后,我把它转换成一个的SelectList:
Dim myRoles As New SelectList(myRoleItems, "Value", "Text")
,创造我Html.DropDownList:
@Html.DropDownList("RoleList", myRoles, "Select Role")
最后处理:
<a href="@Url.Action("RoleHandler", "Account", New With {.RESPONSIBILITY_ID = myRoles.SelectedValue})">Search Buildings</a>
的问题是,什么正在通过。我知道我的最后一行与我的SelectList是正确的,因为如果我添加一个参数来选择列表的结束为了selectedValue,它会传递参数,像这样:
Dim myRoles As New SelectList(myRoleItems, "Value", "Text", 905)
在这种情况下,905获得通过。所以,我相当肯定,我在DropDownList中访问我的SelectList中的值时出错。项目的名称虽然在DDL中正确显示,所以我不确定哪里出错了。
任何帮助将不胜感激,谢谢!
边注:'myRoleItems'是'的IEnumerable'(这是通过用'的DropDownList()'方法需要使用新'的SelectList(myRoleItems“。值“,”文本“)从第一个创建另一个相同的'IEnumerable '是毫无意义的。 –