Model Binding MVC3

Hi,

I am trying to output the values of a form based on hidden input values.

The input fields are like so:


<input type="hidden" name="email[]" value="abc@a.com" />
<input type="hidden" name="email[]" value="abc@b.com" />
<input type="hidden" name="email[]" value="abc@c.com" />
<input type="hidden" name="email[]" value="abc@d.com" />
<input type="hidden" name="email[]" value="abc@e.com" />
<input type="hidden" name="email[]" value="abc@f.com" />
<input type="hidden" name="email[]" value="abc@g.com" />
<input type="hidden" name="email[]" value="abc@h.com" />

The action that my form is being sent to is shown below:


        public ActionResult PreAlertEmail(ICollection<string[]> email)
        {
            return Content(email.ToString());
        }

But that doesn’t work. I get the following error:

Object reference not set to an instance of an object.

Any ideas what i am doing wrong?

Many thanks,

Just a guess, but I’d say with ICollection<string> you are expecting a collection of arrays, when what you want is a collection of strings. Try ICollection<string> instead.

The binder will automatically convert your html input array into a collection so you shouldn’t need to do anything more.

In addition, in your html, drop the in the name: name=“email”.

Please see this document for clearer instructions:
Model Binding To A List