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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:10:57+00:00 2026-05-22T12:10:57+00:00

I have a dropdownlist that will not display the selected value correctly. It does

  • 0

I have a dropdownlist that will not display the selected value correctly. It does have the correct elements in it, has the correct text and values being used but there just doesn’t seem to be a way to make it actually select the correct value.

What I’ve got is:

public class StateViewModel {
    public string Name { get; set; }
    public string Abbr { get; set; }
}

public class ChangeAddressViewModel {
    //edited down to what's needed
    public StateViewModel StateViewModel { get; set; }
    public IEnumerable<StateViewModel> StateViewModels { get; set; }
}

What I’ve tried:

@Html.DropDownListFor(model => model.StateViewModel
    , Model.StateViewModels.Select(x => new SelectListItem {
         Text = x.Name,
         Value = x.Abbr,   
         Selected = x.Abbr == Model.StateViewModel.Abbr
         })
    , @Resource.SelectState)

//i've stepped through the lambda above and the expression 
//x.Abbr == Model.StateViewModel.Abbr 
//did evaluate to true for one of the iterations

@Html.DropDownListFor(model => model.StateViewModel
    , new SelectList(Model.StateViewModels
    , "Abbr"
    , "Name"
    , Model.StateViewModel.Abbr)
    , @Resource.SelectState)
  • 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-22T12:10:58+00:00Added an answer on May 22, 2026 at 12:10 pm

    I see you are specifying a new SelectList :

     @Html.DropDownListFor(model => model.StateViewModel
        , new SelectList(Model.StateViewModels
        , "Abbr"
        , "Name"
        , Model.StateViewModel.Abbr)
        , @Resource.SelectState)
    

    this will render a new instance of the list, not the one you were passing in!Hence you will not have a selected value.

    Try setting it to :

    @Html.DropDownListFor(model => model.SelectedStateID,
                            Model.States
                            , new { id = "ddlStates" })
    

    And in your Controller/ViewModel, populate the SelectList as shown below.I wrote my own example as it was easier. below is the code for the Controller, within the ActionResult :

      //Retrieve the list via LINQ/however and also the SelectedStateID
      var states = ....
    
       var statesList = new SelectList(states, "Abbr", "Name", SelectedStateID);
    
    
       var viewModel = new StateViewModel
        {
            //assign it to the corresponding ViewModel property
            States = statesList ,
                    ...
                    ...
        };
    
        return View( viewModel);  
    

    Your ViewModel would look like this :

     private List<SelectListItem> _states = new List<SelectListItem>();
        public List<SelectListItem> States 
        {
            get
            {
                return (_states );
            }
    
            set
            {
                _states = value;
            }
        }
    
        public Guid SelectedStateID
        {
            get;
            set;
        }
    

    I’m assuming this was your question, and that you didnt want to actually have a new list, and also select a value within that list as a default.

    If that was the case, try this:

     @Html.DropDownListFor(model => model.StateViewModel
        , new SelectList(Model.StateViewModels
                        , "Abbr"
                        , "Name")
    , Model.StateViewModel.Abbr
    , @Resource.SelectState)
    

    I think your closing brace was in the wrong position !

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

Sidebar

Related Questions

I have some html that creates a dropdown list. The list has text values
I have a dropdownlist that has 4 columns but only 3 are 'visible' the
I have a dropdownlist control in my form, this dropdownlist will accept multiple value.I
i have a dropdownlist that populates from the sql server database. populating the list
If I have a DropDownList control that makes up part of a CompositeControl how
I have an ASP.NET DropDownList control that retrieves the first and last name from
I have a dropdownlist inside a gridview, that is loaded from a datasource when
I have a CompositeControl that contains a DropDownList. I have set the AutoPostBack property
I have the following code that sets a cookie: string locale = ((DropDownList)this.LoginUser.FindControl(locale)).SelectedValue; HttpCookie
I have update panel that content check box, textbox, 3 DropDownList with CascadingDropDown extender.

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.