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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:46:44+00:00 2026-05-24T02:46:44+00:00

We found strange behaviour in DropDownListFor (ASP.NET MVC3 release). It selects ViewBag property value

  • 0

We found strange behaviour in DropDownListFor (ASP.NET MVC3 release). It selects ViewBag property value instead of Model property value in dropdown.

Model:

public class Country {
    public string Name { get; set; }
}
public class User {
    public Country Country { get; set; }
}

Controller Index action:

ViewBag.CountryList = new List<Country> {  /* Dropdown collection */
   new Country() { Name = "Danmark" }, 
   new Country() { Name = "Russia" } }; 

var user = new User();
user.Country = new Country(){Name = "Russia"}; /* User value */
ViewBag.Country = new Country() { Name = "Danmark" };  /* It affects user */
return View(user); 

View:

@Html.EditorFor(user => user.Country.Name)      
@Html.DropDownListFor(user => user.Country.Name,
    new SelectList(ViewBag.CountryList, "Name", "Name", Model.Country), "...")

It will show text box with “Russia” value and dropdown with “Danmark” value selected instead of “Russia”.

I didn’t find any documentation about this behaviour. Is this behaviour normal? And why is it normal? Because it is very hard to control ViewBag and Model properties names.

This sample MVC3 project sources

  • 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-24T02:46:45+00:00Added an answer on May 24, 2026 at 2:46 am

    I’m not so sure why this decision was made, but it was happened because MVC framework tried to use the ViewData-supplied value before using the parameter-supplied value. That’s why ViewBag.Country override parameter-supplied value Model.Country.

    That was how it was written in MVC framework in the private method SelectInternal.

    object defaultValue = (allowMultiple) ? htmlHelper.GetModelStateValue(fullName, typeof(string[])) : htmlHelper.GetModelStateValue(fullName, typeof(string));
    
    // If we haven't already used ViewData to get the entire list of items then we need to
    // use the ViewData-supplied value before using the parameter-supplied value.
    if (!usedViewData) {
        if (defaultValue == null) {
            defaultValue = htmlHelper.ViewData.Eval(fullName);
        }
    }
    
    if (defaultValue != null) {
        IEnumerable defaultValues = (allowMultiple) ? defaultValue as IEnumerable : new[] { defaultValue };
        IEnumerable<string> values = from object value in defaultValues select Convert.ToString(value, CultureInfo.CurrentCulture);
        HashSet<string> selectedValues = new HashSet<string>(values, StringComparer.OrdinalIgnoreCase);
        List<SelectListItem> newSelectList = new List<SelectListItem>();
    
        foreach (SelectListItem item in selectList) {
            item.Selected = (item.Value != null) ? selectedValues.Contains(item.Value) : selectedValues.Contains(item.Text);
            newSelectList.Add(item);
        }
        selectList = newSelectList;
    }
    

    This code defaultValue = htmlHelper.ViewData.Eval(fullName); tried to get the value from ViewData and if it can get the value, it will override the supplied parameter selectList with new list.

    Hope it can help. Thanks.

    side-node: ViewBag is just a dynamic wrapper class of ViewData.

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

Sidebar

Related Questions

I found this strange behaviour in .NET and even after looking into CLR via
Have some strange behaviour regarding output caching in an ASP.NET 4 application on IIS
I found strange generics behaviour. In two words - thing I realy want is
I'm hacking my way through learning Flex and have found some strange behaviour. When
I've tried to learn the signal handling in C, when found strange behaviour. When
I found a strange behaviour in an ant build of a git repository. Here's
I moved my website to new server and found strange PDO behaviour: $q =
I've come across a strange behaviour in .NET/Reflection and cannot find any solution/explanation for
I've found some strange behaviour of firefox 3.6 - when I call HtmlPage.Window.Confirm() method
I have found a strange behaviour when trying to push a viewcontroller onto the

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.