2

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.

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

Yes, but as long as you follow the pattern which the DefaultModelBinder understands, you don't have to.
0

I think this kind of thing will work

<input type="text" name="Person[1].name" />

Comments

0

you may need to play around with a custom controlleractioninvoker.

or just a plain modelbinder

Andrew

1 Comment

it will work for any type, "Persons[]" is a type in exactly the same way "Persons" is

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.