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 6874949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:15:33+00:00 2026-05-27T04:15:33+00:00

I’m experimenting with ASP.NET MVC3 and want to simply populate a dropdown list with

  • 0

I’m experimenting with ASP.NET MVC3 and want to simply populate a dropdown list with data I get from a LINQ2SQL class, like so

controller (I know, Linq doesn’t belong in the controller)

var allUsers = (from u in _userDataContext.Users
                            select u).ToList();
            ViewBag.allUsers = allUsers.ToList();
            return View();

view:

<select id="drop_heroes">
        @foreach (var u in ViewBag.allUsers)
        { 
            <option value="@u.pk_userid">@u.email</option>
        }
    </select>

That works fine, but I would like to use Razor @Html.Dropdownlist to create the same dropdown list, but can't find any info to make this work with Linq data. 
  • 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-05-27T04:15:33+00:00Added an answer on May 27, 2026 at 4:15 am

    I know, Linq doesn’t belong in the controller

    Then why are you using it in a controller? Anyway, at least it’s fine that you know it.

    Here’s an example. As always in an ASP.NET MVC application you start by defining a view model which will represent the data that you need in the view. So in your case you need to display a dropdown so you define a list of users and a selected user id:

    public class MyViewModel
    {
        public string SelectedUserId { get; set; }
        public IEnumerable<SelectListItem> Users { get; set; }
    }
    

    then you define a controller action which will populate this view model from your repository and handle it to the view:

    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            Users = _userDataContext.Users.ToList().Select(x => new SelectListItem
            {
                Value = x.pk_userid.ToString(),
                Text = x.email
            })
        }
        return View(model);
    }
    

    and finally you will have a view which will be strongly typed to your view model and use HTML helpers to generate the dropdownlist:

    @model MyViewModel
    @using (Html.BeginForm())
    {
        @Html.DropDownListFor(x => x.SelectedUserId, Model.Users)
        <button type="submit">OK</button>
    }
    

    Things to notice:

    • Usage of view models
    • Usage of a strongly typed view
    • Usage of strongly typed HTML helpers to generate markup such as form elements and input fields
    • Getting rid of weakly typed structures such as ViewBag

    If you follow these simple rules you will see how much easier your life as an ASP.NET MVC developer will become.

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I want to construct a data frame in an Rcpp function, but when I
I am using jsonparser to parse data and images obtained from json response. When
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm interested in microtypography issues on the web. I want a tool to fix:
I am doing a simple coin flipping experiment for class that involves flipping a

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.