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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:59:38+00:00 2026-05-23T17:59:38+00:00

I am using DropDownListFor to render a dropdown list in a view. Somehow the

  • 0

I am using DropDownListFor to render a dropdown list in a view. Somehow the rendered list does not select the SelectListItem with Selected set to true.

In the controller action:

var selectList = sortedEntries.Select(entry => new SelectListItem
                            {
                                Selected = entry.Value.Equals(selectedValue),
                                Text = entry.Value,
                                Value = entry.Id
                            });

return View(new DropDownListModel
            {
                ListId = id,
                SelectList = selectList,
                OptionLabel = "Click to Select"
            });

In the view:

<%= Html.DropDownListFor(m => m.ListId, 
    Model.SelectList, 
    Model.OptionLabel, 
    new {@class="someClass"}) %>

I have tried the following:

  1. make sure that there is one and only one items with Selected set to true.
  2. remove the option label argument.
  3. remove the HTML attribute object.
  4. use SelectList in DropDownListFor:

   Html.DropDownListFor(m => m.ListId, 
        new SelectList(Model.SelectList, "Value", "Text", 
             new List<SelectListItem>(Model.SelectList).Find(s => s.Selected)), 
        new {@class="someClass"})

Any suggestions as to what went wrong?

EDIT:

more information:

  • This action is a child action, called by another view with HTML.RenderAction
  • 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-23T17:59:39+00:00Added an answer on May 23, 2026 at 5:59 pm

    Try like this:

    var selectList = sortedEntries.Select(entry => new SelectListItem
    {
        Text = entry.Value,
        Value = entry.Id
    });
    
    return View(new DropDownListModel
    {
        // The drop down list is bound to ListId so simply set its value
        // to some element value in the list and it will get automatically
        // preselected
        ListId = selectedValue, 
        SelectList = selectList,
        OptionLabel = "Click to Select"
    });
    

    and in the view:

    <%= Html.DropDownListFor(
        m => m.ListId, 
        new SelectList(Model.SelectList, "Value", "Text"), 
        Model.OptionLabel, 
        new { @class = "someClass" }
    ) %>
    

    There could be one more gotcha: you are trying to change the selected value in a POST action. For example you rendered a form, the user selected some value in the dropdown, submitted the form and in your POST action you do some processing on this selected value and when you redisplay the view you want the drop down list to have some other value selected. In this case you will have to remove the initial selection which is contained in the ModelState or the Html helper will ignore the selected value in the model:

    // do this before returning the view and only if your scenario
    // corresponds to what I described above
    ModelState.Remove("ListId");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Html.DropDownListFor to build a select list. It is a simple list
I'm using DropDownListFor in my view: <%: Html.DropDownListFor(model => model.Type, new SelectList(ComponentsFactory.GetTypeList().Select(a => new
Previously, I was using this in my model public List<SelectListItem> StatusList { get; set;
I'm rendering a drop down list using Html.DropDownListFor() extension. The markup I get is
Using ASP.NET MVC 3, I have been struggling to set the selected value for
You can create a dropdownlist using @Html.DropDownListFor(model => model.SelectedId, model.DropDownItems); but where does DropDownListFor
I am using a drop-down list in one of my templates. @Html.DropDownListFor(m => m.LastName,
I have a view that is displaying a Drop down list using the HTML
i have two select using as a dropdownlist for country/state everything works as i
Using TortoiseSVN against VisualSVN I delete a source file that I should not have

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.