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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:56:16+00:00 2026-05-23T02:56:16+00:00

I have been asked to look at a bug in some ASP.Net MVC code

  • 0

I have been asked to look at a bug in some ASP.Net MVC code and have a (to me) very odd problem with a SelectList.

The code from the controller to generate the items (a method to return a SelectList, there are 5 in total). Each SelectList is then saved into the ViewData collection.

List<SelectListItem> items = new List<SelectListItem>();
string yesText = "Yes";
string noText = "No";
if (ci.LCID.Equals((int)LanguageCodes.FRANCE))
{
    yesText = "Oui";
    noText = "Non";
}

SelectListItem yesItem = new SelectListItem();
yesItem.Text = yesText;
yesItem.Value = ((int)MarketingBy.Yes).ToString();
yesItem.Selected = selectedValue != null && selectedValue.Equals(int.Parse(yesItem.Value));

SelectListItem noItem = new SelectListItem();
noItem.Text = noText;
noItem.Value = ((int)MarketingBy.No).ToString();
noItem.Selected = selectedValue != null && selectedValue.Equals(int.Parse(noItem.Value));

items.Add(yesItem);
items.Add(noItem);

return new SelectList(items, "Value", "Text", yesItem.Selected ? yesItem.Value : noItem.Value);

A quick ‘quickwatch’ at the point of creation suggests everything is ok:
Select List generated

At the point the view is being rendered, the values still look ok. However when the view loads, the first item in the list is always selected. The HTML generated is:

<tr>
<td>Fax</td>
<td>
    <select id="MarketingByFax" name="MarketingByFax">
        <option value="134300002">Yes</option>
        <option value="134300001">No</option>
    </select>
</td>
</tr>

(Other values ommitted for clarity).

Any ideas? Or avenues to research? The author is adamant that this was working ‘up til last week’ (I have no idea either way).

Edit: Code for the view –

<td><%: Html.DropDownList("MarketingByFax", (SelectList)ViewData["MarketingByFaxList"])%></td>
  • 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-23T02:56:17+00:00Added an answer on May 23, 2026 at 2:56 am

    This code looks just horrible in every imaginable aspect (IMHO of course). I have no idea why it doesn’t work and I don’t want to know. All I can do is to suggest you how to improve it (so you can stop reading this post if you are looking for a solution about why your code doesn’t work as I have no freaking idea).

    So the first improvement would be to get rid of any ViewData and introduce a view model:

    public class MyViewModel
    {
        public string SelectedValue { get; set; }
        public IEnumerable<SelectListItem> Items { get; set; }
    }
    

    then I would have a controller action that would populate this view model:

    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            // I want to preselect the second value
            SelectedValue = "No",
            Items = new[]
            {
                new SelectListItem { Value = "Yes", Text = "yeap !" },
                new SelectListItem { Value = "No", Text = "nope !" },
            }
        };
        return View(model);
    }
    

    and in my strongly typed view I would simply bind the helper to the view model:

    <%= Html.DropDownListFor(
        x => x.SelectedValue,
        new SelectList(Model.Items, "Value", "Text")
    ) %>
    

    Also if you want to work with some enum types you may find the following extension method useful.

    See how easy it is? No more ugly casts with ViewData, no more need to define any lists and specify some complicated conditions, …

    Remark: once again, those are just my 2¢, you can continue the combat with ViewData if you will.

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

Sidebar

Related Questions

I have been asked to develop some usercontrols in ASP.NET that will at a
We have been asked to look into a fully open source solution for a
I have been asked to convert some Lotus 123 wk4 files with macros to
I have been asked to look at how to restrict read access on certain
I have been asked to look into FileMaker for creating a pretty simple database
I have been asked to look into writing an application that will be a
i have been asked the php mail() function, is it send from linux ?or
I have been asked to lend a hand on a hobby project that a
I have been asked to standardize the screen saver and desktop background used by
I have been asked to audit any/all changes in a MySQL table. Does anyone

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.