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

  • Home
  • SEARCH
  • 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 8725561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:04:02+00:00 2026-06-13T08:04:02+00:00

I am having an issue where the item in the dropdown list is not

  • 0

I am having an issue where the item in the dropdown list is not being selected. I am building up an array of SelectListItems and setting the right one to selected = true but its not getting selected.

I have the following code in my controller:

    public ActionResult Edit(int id)
    {
        var p = _myRepository.FindBy(id);
        var vm = new MyViewModel(p) { CategoryTypes = ControllerUtils.GetList(_myTypeRepository, r => r.Name, p.CategoryType.Id) };

        return View(vm);
    }

and here is the GetList function in the ControllerUtils class:

public static class ControllerUtils
{
    public static IEnumerable<SelectListItem> GetList<T>(IIntKeyedRepository<T> list, Func<T, string> getName, int id) where T : BaseModel
    {
        var items = list.All().ToList();
        var itemsList = items.Select(r => new SelectListItem() { Selected = r.Id == id, Text = getName(r), Value = r.Id.ToString() });
        return itemsList;
    }
}

and here is my view code:

    <div class="editor-label">
        @Html.LabelFor(model => model.MyObject.CategoryType)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.MyObject.CategoryType, Model.CategoryTypes)
        @Html.ValidationMessageFor(model => model.MyObject.CategoryType)
    </div>

When i debug in the controller code and list into the array of SelectListItem objects i do see the second item has selected = true. But when i check the view HTML, I don’t see either item being selected:

<select id="CategoryType" name="CategoryType">
  <option value="1">Choice 1</option>
  <option value="2">Choice 2</option>
</select>

As you can see, there is no “selected=”selected” for either item. Any suggestions on what could be going on here?

  • 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-06-13T08:04:03+00:00Added an answer on June 13, 2026 at 8:04 am

    The first argument of the DropDownListFor helper must be a lambda expression pointing to a primitive type property on your view model containing the selected value:

    @Html.DropDownListFor(
        model => model.MyObject.CategoryType.Id, 
        Model.CategoryTypes
    )
    

    Now get rid of the Selected = r.Id == id property inside your SelectListItem. You don’t need it. Now assuming that Model.MyObject.CategoryType.Id has a corresponding item with matching value inside Model.CategoryTypes the helper will automatically preselect this item.

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

Sidebar

Related Questions

I am having an issue with line-height on a list item. In a nutshell,
I'm having an issue with my rounded navigation, the first list item appears to
I having an issue where the ImageButton background is being drawn with a different
I am having issues with populating a telerik dropdown list using jquery. I get
I'm having an issue with a ListView not showing the latest details of the
I'm having a Firefox issue. On Chrome for example, when I select an item,
I am having an issue with EmailReceived method with a custom list. After creating
I am having any issue with selecting any item from the drop down. Below
I think I'm having boxing issues foreach(var p in item.GetType().GetProperties(). Where(p => p.GetValue(original, null)
Having issue with slider navigation. Works perfectly fine when there are no other unordered

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.