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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:52:02+00:00 2026-06-16T16:52:02+00:00

Getting some very annoying behaviour from my MVC3 app. So I’ve got a model

  • 0

Getting some very annoying behaviour from my MVC3 app. So I’ve got a model with 3 values:

[DisplayName("Airlines ")]
[StringLength(2, ErrorMessage = "Airline codes can only be 2 characters in length")]
public string AirlineCode1 { get; set; }

[DisplayName("Airlines")]
[StringLength(2, ErrorMessage = "Airline codes can only be 2 characters in length")]
public string AirlineCode2 { get; set; }

[DisplayName("Airlines")]
[StringLength(2, ErrorMessage = "Airline codes can only be 2 characters in length")]
public string AirlineCode3 { get; set; }

Now these are populated from a DropDowList so I’m popping the DropDownListItems in the ViewBag and rendering them in the view such:

@Html.LabelFor(l => l.AirlineCode1) <span>(select/enter code in box on right)</span>
<div id="airportCode1">
    @Html.DropDownListFor(d => d.AirlineCode, ViewBag.AirLines as List<SelectListItem>) <input type="text" maxlength="2" value="@Model.AirlineCode1" />
</div>
<div id="airportCode2" style="@Model.AirlineCode2Style">
     @Html.DropDownListFor(d => d.AirlineCode, ViewBag.AirLines2 as List<SelectListItem>) <input type="text" maxlength="2" value="@Model.AirlineCode2" />
</div>
<div id="airportCode3">
    @Html.DropDownListFor(d => d.AirlineCode3, ViewBag.AirLines3 as List<SelectListItem>) <input type="text" maxlength="2" value="@Model.AirlineCode3" />
</div>

So my controller looks like:

IEnumerable<SelectListItem> airLines = PopulateAirlines(user);
ViewBag.AirLines = airLines;
ViewBag.AirLines2 = airLines;
ViewBag.AirLines3 = airLines;

Now in some circumstances I want to prepoulate AirLineCode in the model. so I set the model value in the controller. This resulted in some odd behaviour. Suddenly all my DropDownLists contained the prepopulated value!

Checked the model, value only set in AirLineCode1. Checked the ViewBag, no selected SelectListItems. So I figured the ViewBag must be maintaining a reference. So I changed my code to:

ViewBag.AirLines = PopulateAirlines(user);
ViewBag.AirLines2 = PopulateAirlines(user);
ViewBag.AirLines3 = PopulateAirlines(user);

Boom, fixed. Problem is PopulateAirlines is an expensive process!

Problem is the ViewBag appears to be maintaing a reference between the 3 SelectListItem Lists. How do I stop it doing this and still only make one call to PopulateAirlines(user);?

I tried the below code and this completely blew up:

IEnumerable<SelectListItem> airLines = PopulateAirlines(user);
ViewBag.AirLines = airLines;
ViewBag.AirLines2 = airLines.Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Selected });
ViewBag.AirLines3 = airLines.Select(s => new SelectListItem() { Text = s.Text, Value = s.Value, Selected = s.Selected });

with the error:

There is no ViewData item of type ‘IEnumerable’ that
has the key ‘AirlineCode2’.

What??!

  • 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-16T16:52:03+00:00Added an answer on June 16, 2026 at 4:52 pm

    It may be because of in this code:

    IEnumerable<SelectListItem> airLines = PopulateAirlines(user);
    ViewBag.AirLines = airLines;
    ViewBag.AirLines2 = airLines;
    ViewBag.AirLines3 = airLines;
    

    all the ViewBag properties are referring to the same list. A change to the list will therefore reflect when referencing any of the properties. When you call the PopulateAirlines() method you’re creating 3 different lists.

    You’ll need make three separate lists but only create the first using the PopulateAirlines method and then clone it twice. That will not be as expensive.

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

Sidebar

Related Questions

I am getting some very annoying behaviour from my perl cgi scripts running under
I'm getting some very strange behaviour using the following switch statement: string recognise_mti(int mti_code)
Hey there, i got this very annoying problem: I have a CheckBoxList getting items
I'm getting some very strange behaviour with tinyMCE in an ASP.NET MVC 2 beta
I have been getting some massive head aches working on a very dynamic app.
I'm getting some very strange behaviour with HttpWebRequest I hope someone can help me
i expected to see numbers from 0.0 to 999.0 but instead getting some very
I am getting some very interesting results when testing an app developed with MonoDroid
I've got some very odd behaviour going on. I have the following JQuery: myElement.fadeOut(100);
I'm getting some very slow performance from an Umbraco site hosted on my Azure

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.