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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:14:30+00:00 2026-05-26T04:14:30+00:00

I can’t seem to set the Selected item in an Html.DropDownListFor when the following

  • 0

I can’t seem to set the Selected item in an Html.DropDownListFor when the following conditions exist:

1) The selected item ID is drawn from an enum (e.g., (int)AnimalType), and

2) The list is populated from a database (or, actually, any list other than the enum)

For example, in this untested pseudocode, the View’s DropDownList would not have the animal Dog selected. Note that if I change the enum to a static class that produces int values, I have no problem. In the Linq select statement, if I try to cast a.AnimalType to (int)a.AnimalType, the compiler complains. Any ideas?

Thanks.

    //--------------In the model
    enum AnimalType
    {
            Dog = 1,
            Cat = 2,
            //etc.
    }

    public class Animal
    {
            public AnimalType AnimalId {get;set;}
            public string Name {get;set;}
            //etc.
    }
    public class AnimalModel
    {
            public AnimalId SelectedAnimal {get;set;}
            public IEnumerable<SelectListItem> AllAnimals {get;set;}
    }

    //--------------In the controller

    AnimalModel model = new AnimalModel();
    model.SelectedAnimal = (AnimalType)1;

    List<Animal> getAllAnimals = Repository.GetAllAnimals();//defined elsewhere

    IEnumerable<SelectListItem> animalList =
                    from a in getAllAnimals
                    select new SelectListItem
                    {
                        Selected = (a.AnimalType == (int)model.SelectedAnimal),
                        Text = a.Name,
                        Value = a.AnimalId.ToString()
                    };
    model.AllAnimals = animalList;

    //--------------In the view

    @Html.DropDownListFor(m => m.Id, Model.AllAnimals)
  • 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-26T04:14:31+00:00Added an answer on May 26, 2026 at 4:14 am

    Try using an integer as the selected animal:

    public class AnimalModel
    {
        public int SelectedAnimal { get; set; }
        public IEnumerable<SelectListItem> AllAnimals { get; set; }
    }
    

    and then in your controller:

    public ActionResult Index()
    {
        var animals = Repository.GetAllAnimals();
        var model = new AnimalModel
        {
            // define which item should be selected in the drop down
            SelectedAnimal = (int)AnimalType.Cat,
    
            // define the list of items in the drop down
            AllAnimals = animals.Select(x => new SelectListItem
            {
                Value = x.AnimalId.ToString(),
                Text = x.Name
            })
        };
        return View(model);
    }
    

    and finally in the view:

    @model AnimalModel
    
    @Html.DropDownListFor(x => x.SelectedAnimal, Model.AllAnimals)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can you cast a List<int> to List<string> somehow? I know I could loop through
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can anyone give me an example of how to return the following json simply
Can anybody tell what is the best(easy) way to sort the following string 'index'
Can MOSS integrate and get user profiles from multiple Active Directory and/or LDAP stores?
Can a Visual Studio 2008 custom tool be passed additional parameters from the custom
Can anyone direct me to a good tutorial on how to set up virtual
Can you have submenus with the top level set to checkable in WPF? I
Can't seem to get the Back Button to appear in a UINavigationController flow. I
Does anyone know how can I replace this 2 symbol below from the string

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.