views:

477

answers:

4

I would like to have the following signature of a MVC controller.

public ActionResult Create(Persons[] p)
{

}

Is it possible to have something like this?

EDIT: Let's say I would like to obtain an array of person objects by submitting a list of names separated by a special symbol. For example I submit a form

<form>
   <input name="person_name">Max|Alex|Andrew</input>
</form>

I suppose it should be done by implementing a IModelBinder interface but I didn't find any example how to do this.

A: 

I think this kind of thing will work

<input type="text" name="Person[1].name" />
David Archer
A: 

you may need to play around with a custom controlleractioninvoker.

or just a plain modelbinder

Andrew

Andrew Bullock
Nope. It works only for single value arguments.
Jenea
it will work for any type, "Persons[]" is a type in exactly the same way "Persons" is
Andrew Bullock
+2  A: 

Yes. See this question for how to get collections binded by the DefaultModelBinder.

çağdaş
Is it possible to make this work via custom model binder?
Jenea
Yes, but as long as you follow the pattern which the DefaultModelBinder understands, you don't have to.
çağdaş
+3  A: 

Oh I've found how to do this.

In the Global.asax file in the Application_Start() function should be written:

ModelBinders.Binders(typeof(Person[])) = new PersonBinder();

In this manner a custom binder is registered.

Jenea