2

I am trying to use the Html.ListBoxFor helper to show a list box and return the selected Id. Is there a problem with the dataValueField not being a string?

If the SelectList contained in the model uses integers as the dataValueField then I get a "Value cannot be null - Parameter name: Source" exception raised when the list is rendered in the view.

If the Id is changed to a string then everything works and the selected Id is passed back to the view.

Any ideas?

Here is the controller (based on a cut down new project)

namespace Mvc2.Controllers
{
    public class ViewModel
    {
        public int TestId { get; set; } // if this is a string it works ok
        public SelectList ListData {get; set;}
    }

    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new ViewModel();
            model.TestId = 1; // code corrected after Lazarus' comment

            var lst = new[] { new { Id = 1, Name = "cat" }, new { Id = 2, Name = "dog" } };
            model.ListData = new SelectList(lst, "Id", "Name");

            return View("TestView", model);
        }

        public ActionResult TestSubmit(ViewModel returnedModel)
        {
            int i = 99; // break here - returnedModel has correct TestId when declared as string
        }
    }
}

here is the View - crashes on the ListBoxFor line

<%using (Html.BeginForm("TestSubmit", "Home")) { %>

    <%=Model.TestId %><br />
    <%=Html.ListBoxFor(m => m.TestId, Model.ListData) %>

    <br />
    <input type="submit" value="Save" />

<%} %>

3 Answers 3

3

The expression you are passing for the selected values needs to be IEnumerable because ListBoxFor supports multiple selected items.

1
  • 2
    thats right.. if you only want to have an single int/id then you should use DropDownListFor instead Jul 22, 2011 at 17:48
2

Answering my own question;

I am unconviced by the comments that this might be a bug which is waiting to be fixed because I get it in RC2 and in MVC 1 (I copied the code back to a project in that release). Anyway I have implemented a work around for now which is to:-

(a) Add a dummy string version of the Id to the model (TestId)

public class ViewModel
{
    public string TestId { get; set; } // dummy Id as a string
    public List<DataToShow> Data { get; set; }
    public SelectList ListData {get; set;}
}

(b) Display the list but retrieve the value as the dummy TestId - note that the list still dumps the data values as integers!

<%=Html.ListBoxFor(m => m.TestId, Model.ListData) %>

(c) Copy the dummy string value into its proper integer location in the action

public ActionResult TestSubmit(ViewModel returnedModel)
{
    MyModel.DataId = Int32.Parse(returnedModel.TestId);

Hope this is of some Help.

1
  • What is TestId for? Are you passing and presenting multiple selects on the page?
    – Lazarus
    Feb 20, 2010 at 8:20
1

This is a known issue with ASP.NET MVC 2. It should be fixed in the March release.

3
  • I got this bug about 2 nights ago and I have the newest release. (Well, RC 2). Is there a newer release that I'm missing? Feb 18, 2010 at 18:19
  • No, but from your description elsewhere you're having a different issue (maybe in your code, maybe in MVC code). Also, I'm close to certain that the bug Levi describes isn't causing the behavior here. No FormCollection is involved (yet), and the parameter has a different name. You can look at FormCollection.GetValue() in the RC 2 source to confirm that the string.IsNullOrEmpty() is, indeed, gone. Feb 18, 2010 at 18:56
  • I just did this yesterday and saw the exact same behavior. Steve's workaround worked for me as well. Mar 4, 2010 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.