ASP.NET MVC: getting form fields with the same name

Is there a way to get form fields with the same name, other than using templates or comma separation.

I have several text fields with the same name, and I need to skip them and get each value.

thanks

+2
source share
2 answers

FormCollection is a NameValueCollection. This means that you can do:

public ActionResult MyAction(FormCollection form) { // ModelBinder will set "form" appropriately foreach(var value in form.Getvalues("duplicatedFieldname")) { //do something with value } } 
+4
source

Even simpler:

 public ActionResult MyMethod(string[] fieldName) 

Or use List<string> if you prefer this over string[] .

+4
source

All Articles