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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:54:18+00:00 2026-05-26T04:54:18+00:00

Setup: I have scaffolded a controller using MvcScaffolding. For a property, Model.IdCurrencyFrom, the scaffolding

  • 0

Setup:

I have scaffolded a controller using MvcScaffolding.

For a property, Model.IdCurrencyFrom, the scaffolding created an Html.DropDownListFor:

@Html.DropDownListFor(model => model.IdCurrencyFrom, 
    ((IEnumerable<FlatAdmin.Domain.Entities.Currency>)ViewBag.AllCurrencies).Select(option => new SelectListItem {
        Text = (option == null ? "None" : option.CurrencyName), 
        Value = option.CurrencyId.ToString(),
        Selected = (Model != null) && (option.CurrencyId == Model.IdCurrencyFrom)
    }), "Choose...")

This works fine, both with new records, or editing existing ones.

Problem:

There are only 3 currencies, AR$, US$ and GB£. So, instead of a drop down list, I want a ListBox.

So I changed the above to:

@Html.ListBoxFor(model => model.IdCurrencyFrom, 
    ((IEnumerable<FlatAdmin.Domain.Entities.Currency>)ViewBag.AllCurrencies).Select(option => new SelectListItem {
        Text = (option == null ? "None" : option.CurrencyName), 
        Value = option.CurrencyId.ToString(),
        Selected = (Model != null) && (option.CurrencyId == Model.IdCurrencyFrom)
    }))

I now get an ArgumentNullException, Parameter name: source, but only when editing an existing record. Creating new records, this works fine.

Questions:

What is happening?!

Nothing has changed. Switching back to DropDownListFor and it all works fine. Switching to ListBox (as opposed to ListBoxFor) and I get the error.

The model is not null (like I said, it works fine with the DropDownListFor)… and I’ve run out of ideas.

  • 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-26T04:54:19+00:00Added an answer on May 26, 2026 at 4:54 am

    I’ve checked the source of the HTML helpers, it was a fun exercise.

    TL;DR;
    The problem is that ListBoxFor is for multiple selection and it expects an enumerable Model property. Your Model property (model.IdCurrencyFrom) is not an enumerable that’s why you get the exception.

    Here are my findings:

    1. The ListBoxFor method will render a select element with multiple="multiple" attribute, always. It is hard coded in System.Web.Mvc.Html.SelectExtensions

      private static MvcHtmlString ListBoxHelper(HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, IDictionary<string, object> htmlAttributes) {
          return SelectInternal(htmlHelper, null /* optionLabel */, name, selectList, true /* allowMultiple */, htmlAttributes);
      }
      

      So maybe you anyway don’t want to allow for the user multiple currencies…

    2. Your problem starts when this ListBoxHelper tries to get the default value from your model property:

      object defaultValue = (allowMultiple) ? htmlHelper.GetModelStateValue(fullName, typeof(string[])) : htmlHelper.GetModelStateValue(fullName, typeof(string)); 
      

      It works for DropDownList because it passes false to allowMultiple when calling SelectInternal.
      Because your ViewData.ModelState is empty (because there were no validation occurred in your controller before) the defaultValue will be null. Then defaultValue gets initialized with your model’s default value (your case model.IdCurrencyFrom is int I guess) so it will be 0. :

      if (!usedViewData) {
              if (defaultValue == null) {
                  defaultValue = htmlHelper.ViewData.Eval(fullName);
              } 
       }
      

      We are getting close to the exception 🙂 Because as I mentioned ListBoxFor only support multiple selection, so it tries to handle defaultValue as IEnumbrable:

      IEnumerable defaultValues = (allowMultiple) ? defaultValue as IEnumerable : new[] { defaultValue };
      IEnumerable<string> values = from object value in defaultValues select Convert.ToString(value, CultureInfo.CurrentCulture); 
      

      And in the second line there is your ArgumentException because defaultValues is null.

    3. Because it expects defaultValue to be enumerable and because string is enumerable. If you change the the type of model.IdCurrencyFrom to string it will work. But of course you will have multiple selection on the UI but you will only get the first selection in your model.

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

Sidebar

Related Questions

Setup: I have an HTML page with a fieldset element. The background color of
I have setup a property and implement INotifyPropertyChanged like so... public event PropertyChangedEventHandler PropertyChanged;
I have setup a custom managed property in MOSS and mapped it to a
Using Ruby on Rails, I have created a blog. The blog has posts, and
I have a Rails 3 app with authentication setup using Devise with the registerable
I have setup my app to launch Google maps to a set location using
I have setup a VM (VMWEB-3) to test local development of Orchard CMS using
I have setup a simple test project for the EF 4.1 RC, using Code-First
We have setup a webform using webform for marketer module in sitecore. Using this
I have setup some views with the navigation controller. I want to be able

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.