tags:

views:

83

answers:

5

Can i use List or something?

+4  A: 

Scott Hanselman has an excellent tutorial for doing this here.

Robert Harvey
Phil Haack has an updated version, with a download project. http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
Khalid Abuhakmeh
+1  A: 

Just take the right sort of collection. Exactly which sort depends on which version:

MVC1 : public ActionResult DoSomething(int[] input)
MVC2 : public ActionResult DoSomething(IList<int> input)

Wyatt Barnett
You can pass a `List<int>` into an action method in MVC 1.
DaveDev
+1  A: 
 [ArrayOrListParameterAttribute("ids", ",")]
 public ActionResult Index(List<string> ids)
 {

 }
apolfj
+1  A: 

You need to pass them to your action via adding each integer to your POST or GET querystring like so:

myints=1&myints=4&myints=6

Then in your action you will have the following action

public ActionResult Blah(List<int> myints)

MVC will then populate the list with 1,4, and 6

One thing to be aware of. Your query string CANNOT have brackets in them. Sometimes when the javascript lists are formed your query string will look like this:

myints[]=1&myints[]=4&myints[]=6

This will cause your List to be null (or have a count of zero). The brackets must not be there for MVC to bind your model correctly.

KallDrexx
+1  A: 

If you are trying to send the list from some interface item (like, a table), you can just set their name attribute in the HTML to: CollectionName[Index] for example:

<input id="IntList_0_" name="IntList[0]" type="text" value="1" />
<input id="IntList_1_" name="IntList[1]" type="text" value="2" />

And

public ActionResult DoSomething(List<int> IntList) {
}

The IntList parameter wil receive a list containing 1 and 2 in that order

Tejo