2

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?

Inez
  • 2,115
  • 3
  • 20
  • 25

3 Answers3

5

Making a list of a comma seperated string :

var myList = mytextbox.text.Split(',').ToList();  
Peter
  • 47,963
  • 46
  • 132
  • 181
5

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){....}
hackerhasid
  • 11,699
  • 10
  • 42
  • 60
1

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 The Lion
  • 61,704
  • 67
  • 242
  • 415