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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:24:27+00:00 2026-05-13T09:24:27+00:00

This question relates to ASP.NET MVC 2 RC (December drop). The basic problem is

  • 0

This question relates to ASP.NET MVC 2 RC (December drop). The basic problem is that when creating a drop down list with a default selected value, the “selected” attribute is either not rendered to HTML or attributed to the wrong option.

Here’s some code to demonstrate the wrong option issue:

Here’s a function to generate some values [AttributeService class]:

public static Dictionary<int, string> MonthOfBirth
    {
        get
        {
            Dictionary<int, string> months = new Dictionary<int, string>();
            for (int i = 0; i <= 11; i++)
            {
                months.Add(i, CultureInfo.CurrentUICulture.DateTimeFormat.MonthNames[i]);
            }
            return months;
        }
    }

Here’s an extension helper to create a list of SelectListItems:

public static List<SelectListItem> ToSelectList<T>(
        this IEnumerable<T> enumerable,
        Func<T, string> text,
        Func<T, int> value,
        string defaultOption
        )
    {
        var items = enumerable.Select(x => new SelectListItem
        {
            Text = text(x),
            Value = value(x).ToString(),
            Selected = false
        }).ToList();

        items.Insert(0, new SelectListItem
        {
            Text = defaultOption,
            Value = "-1",
            Selected = true
        });

        return items;
    }

This snippet ties the two above together:

 public List<SelectListItem> MonthOfBirthList
    {
        get
        {
            return AttributeService.MonthOfBirth.OrderBy(x => x.Key).ToSelectList(x => x.Value, x => x.Key,
                                                 "Month");
        }
    }

The first two snippets reside in various classes and the third lives in my ViewModel which the Controller just spits out to the View. So in my View I then have:

<%=Html.DropDownListFor(x=>x.MonthOfBirth, Model.MonthOfBirthList, new { @class = "panel_4_month" })%>

Note that my extension method adds a default SelectListItem in at position index 0, with a default value and Selected = true. When I put a break point on the Controller to see what the model contains the list is ordered correctly and the correct option is set to selected.

The HTML that I actually get though is this:

<select class="panel_4_month" id="MonthOfBirth" name="MonthOfBirth">
  <option value="-1">Month</option>
  <option selected="selected" value="0">January</option>
  <option value="1">February</option>
  <option value="2">March</option>
  etc...
</select>

Primary question then is whether my code is wrong or whether there’s a bug in the HTML renderer? I’m happy to upload a VS28K solution with sample code somewhere if this isn’t enough information. Thanks.

  • 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-13T09:24:28+00:00Added an answer on May 13, 2026 at 9:24 am

    I can’t see your controller method, but I believe the issue is there. If you set the following in your controller you should see Month Selected.

    public ActionResult Edit(string id)
    {
      User myModel = UserServiceFactory.GetUser(id);
      myModel.MonthOfBirth = -1;
      return View(myModel);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 307k
  • Answers 307k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can call the function with a lambda expression: private… May 13, 2026 at 9:31 pm
  • Editorial Team
    Editorial Team added an answer Is size a column in your chassis_sizes table? If so,… May 13, 2026 at 9:31 pm
  • Editorial Team
    Editorial Team added an answer Changed myChart.Series["mySeries"].ChartType = SeriesChartType.Bar; to myChart.Series["mySeries"].ChartType = SeriesChartType.Column; May 13, 2026 at 9:31 pm

Related Questions

I've found many similar questions but nothing with quite the answer I'm after. Part
I have certain panels on my page that are hidden under certain circumstances. For
I've been trying to implement jQuery's grid function in my Asp.Net MVC app. I'm
I am trying to upload files to a web server using System.Net.WebClient.UploadFile but I
Does anyone have any advice/information on how to install nUnit with ASP.Net MVC? I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.