我有模型来代表用户。我创建了一个帮助类来返回一组这些用户,如List(Of Users)
.
这是我的做法
Function getDoctorsList() As List(Of Users)
Dim userCollection As New List(Of Users)
Dim sql = "SELECT * FROM " + _tblName + " WHERE usertype = 'doctor'"
Dim dr As SqlDataReader = dbHelper.ExecuteAndGetReader(sql)
While dr.Read
Dim user As New Users
user.Id = IIf(IsDBNull(dr("id")), 0, dr("id"))
user.UserName = IIf(IsDBNull(dr("username")), "", dr("username"))
user.UserNin = IIf(IsDBNull(dr("user_nin")), 0, dr("user_nin"))
user.UserType = IIf(IsDBNull(dr("usertype")), "", dr("usertype"))
user.Password = IIf(IsDBNull(dr("password")), "", dr("password"))
userCollection.Add(user)
End While
Return userCollection
End Function
我抓住它们,然后将它们发送到从控制器中查看,如下所示:
ViewData("LoginUserModel") = New LoginUser
ViewData("doctorList") = usersHelper.getDoctorsList
现在,模型有两个属性Id
,UserName
我想分别用作值和文本。
我尝试过这样的事情,但这会产生类型转换问题。所以我显然做错了。
<%= Html.DropDownList("doctors", ViewData("doctorList")) %>
如何将列表作为 SelectListItems 传递给 DropDownList?