2

Is there are way to retrieve form fields with the same name besides using modelbinders or comma splitting.

I have a few textfields with the same name and i need to loop through them and retrieve each value.

Thank you

1
  • 1
    can I ask why do you want form fields with the same name? Commented Jun 12, 2009 at 2:31

2 Answers 2

4

FormCollection is a NameValueCollection. That means you can do:

public ActionResult MyAction(FormCollection form)
{
  // ModelBinder will set "form" appropriately
  foreach(var value in form.Getvalues("duplicatedFieldname"))
  {
    //do something with value
  }
}
Sign up to request clarification or add additional context in comments.

Comments

4

Even easier:

public ActionResult MyMethod(string[] fieldName)

Or use List<string> if you prefer that instead of string[].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.