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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:30:21+00:00 2026-05-16T02:30:21+00:00

I have this to populate a drop down list in an ASP.NET MVC view.

  • 0

I have this to populate a drop down list in an ASP.NET MVC view.

<%= Html.DropDownListFor(model => model.Bikes,
      Model.Bikes.Select(
      x => new SelectListItem {
               Text = x.Name,
               Value = Url.Action("Details", "Bike", new { bikeId = x.ID }),
               Selected = x.ID == Model.ID,
           })) %>

Debugging this I can see that the Selected property is set to true when it should be. But when the view is rendered, none of the options in the list is selected. I realize that this could be done with another overload of DropDownListFor but I really want to get this version working.

Any ideas?

  • 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-16T02:30:22+00:00Added an answer on May 16, 2026 at 2:30 am

    The reason your selected value doesn’t work is because you are using Urls as option values but specifying Model.ID in the Selected clause.

    Try like this:

    <%= Html.DropDownListFor(
        model => model.Bikes,
        new SelectList(Model.Bikes, "Id", "Name", Model.ID)
    )%>
    

    "Id" indicates the property that will be used as option value while "Name" indicates the property that will be used as option label.

    If you want to preserve the Url.Action you could try:

    <%= Html.DropDownListFor(
        model => model.Bikes,
        new SelectList(Model.Bikes.Select(x => new {
            Id = x.Id,
            Name = Url.Action("Details", "Bike", new { bikeId = x.ID })
        }), "Id", "Name", Model.ID)
    )%>
    

    You will notice that I’ve inverted the Name and Id as it seemed more logical to use the model Id as option value.


    UPDATE:

    The reason this doesn’t work is because you are binding to an IEnumerable (the first argument of the DropDownListFor helper). It should be a scalar property while you are using the same model.Bikes collection:

    <%= Html.DropDownListFor(
        model => model.SelectedBikeValue,
        Model.Bikes.Select(
            x => new SelectListItem {
                     Text = x.Name,
                     Value = Url.Action("Details", "Bike", new { bikeId = x.ID }),
                     Selected = x.ID == Model.ID,
            }
    )) %>
    

    My remark about not using Urls as option values stands true.

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

Sidebar

Related Questions

I am using asp.net mvc 2.0(default binding model) and I have this problem. I
In my ASP.NET MVC app, I have a fairly complex edit page which combines
I have an ASP.NET MVC project in C# using Forms Authentication and Active Directory
I've got a page which will have about ten drop down lists which are
How do most developers handle Typed Views in ASP.NET MVC when dealing with large
Summary: If you use ASP.NET MVC 2's templates to render a DropDownList, how would
In my ASP.NET MVC 2 (RC) project - I'm using AutoMapper to map between
I'm sure this has been answered before, but I've spent the last three hours
I have a droplist, and I would like to add an All as the

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.