tags:

views:

74

answers:

3

Let's say that I have field called Customers

<input type="text" name="Customers"

and I want to enter in it comma separated IDs of my Customers and then receive it on ASP.NET MVC side as a List. Is this feature build in ASP.NET MVC, if not what's the best way to do it?

+4  A: 

you have to give your input type an id so you can access it in your code behind.

Then you could do something like this:

List<string> mylist = new List<string>();
string[] mystrings;
string text = mytextbox.text;

mystring = text.Split(',');

foreach (string str in mystrings)
    mylist.Add(str);
Tony
+3  A: 

Making a list of a comma seperated string :

var myList = mytextbox.text.Split(',').ToList();  
Peter
A: 

You could either have a model binder that does a split (like Peter mentioned), or you could use JavaScript to add hidden fields with the same name for all the values. Something like:

<input type="hidden" name="customers" value="102" />
<input type="hidden" name="customers" value="123" />
<input type="hidden" name="customers" value="187" />
<input type="hidden" name="customers" value="298" />
<input type="hidden" name="customers" value="456" />

Then your action would take the enumerable of int like:

public ActionResult DoSomethingWithCustomers(IEnumerable<int> customers){....}
statichippo