Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8421963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:18:00+00:00 2026-06-10T03:18:00+00:00

I have Model to represent User. I have created a helper class to return

  • 0

I have Model to represent User. I have created a helper class to return a set of these Users, as List(Of Users).

Here is how I do that

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

I grab them, and send them to view to from the controller like this:

ViewData("LoginUserModel") = New LoginUser
ViewData("doctorList") = usersHelper.getDoctorsList

Now, Model has two properties Id and UserName I would like to use as a value and text respectively.

I tried something like this, but this gives type casting problems. SO I am obviously doing it wrong.

<%= Html.DropDownList("doctors", ViewData("doctorList")) %>

How to pass the List as SelectListItems to the DropDownList?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:18:02+00:00Added an answer on June 10, 2026 at 3:18 am

    You could use LINQ to project your List(Of User) into an IEnumerable(Of SelectListItem):

    ViewData("doctorList") = usersHelper
        .getDoctorsList()
        .Select(Function(x) New SelectListItem With { .Value = x.Id.ToString(), .Text = x.UserName })
    

    or you could also use the SelectList constructor:

    ViewData("doctorList") = New SelectList(usersHelper.getDoctorsList(), "Id", "UserName")
    

    and inside your view:

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

    but I would rather recommend you using view models instead of ViewData.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a Reply class: class Reply(models.Model): reply_to = models.ForeignKey(New) creator = models.ForeignKey(User) reply
I have a User model that, in my mind, has two show actions: A
In my Yii application, I have a model that represents siteconfig table and have
I have a data model that represents a weekly order. Eg. my order might
I have problem with keeping my model (represented as List) in sync with a
I have model Article it has field title with some text that may contain
I have model public class UploadOptionModel { public UploadOptionModel() { OutputFormat = outputFormatList.Select(i =>
I'm using ASP.NET MVC and I have a model class which represents a peice
I have an application that requires: user owns many projects. project has one owner.
I have two entities, projects and users. These are modeled in Rails using Mongoid

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.