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?

3 Answers 3

5

Making a list of a comma seperated string :

var myList = mytextbox.text.Split(',').ToList();  
Sign up to request clarification or add additional context in comments.

Comments

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){....}

Comments

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);

Comments

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.