C. Ross
C. Ross

Reputation: 31878

How to gather arbitrary length list data in ASP.NET MVC

I need to gather a list of items associated with another item from my user in a ASP.NET MVC project. I would like to have a controller action like bellow.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(int x, int y, IEnumerable<int> zKeys)
{
    //Do stuff here
}

How can I setup my form to pass data in this way? If data of this particular form can't be provided, what's the next best way to pass this type of information in ASP.NET MVC?

Upvotes: 1

Views: 173

Answers (2)

Robert Harvey
Robert Harvey

Reputation: 180948

Scott Hanselman has an excellent article on how to do this here:

ASP.NET Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries http://www.hanselman.com/blog/...BindingToArraysListsCollectionsDictionaries.aspx

Upvotes: 1

omoto
omoto

Reputation: 1220

<form action="/url" method="post">
<input name="x" type="text" value="1" />
<input name="y" type="text" value="1" />

<div>
     <input name="zKeys" value="1" />
     <input name="zKeys" value="2" />
     <input name="zKeys" value="3" />
     <input name="zKeys" value="4" />
     <input name="zKeys" value="5" />
     <input name="zKeys" value="6" />
     <input name="zKeys" value="7" />
</div>

Upvotes: 0

Related Questions