spike.y
spike.y

Reputation: 399

Enumerate all values sent from a form

I am trying to submit a number of values on a webpage using a form. The problem is there are so many values, and I don't know that names of them because they are dynamically generated, so I can't manually enter something like ...Request.Form["someName"] to get its value.

So I need to do something like this:

foreach(var item in Request.Form.AllKeys)
{
    // Do something with values, like:
    insert item.Value into database (for example).
}

But the problem is, I don't think AllKeys is the right thing to be using here. How can I enumerate all of the values that are sent from the form?

Upvotes: 2

Views: 67

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 156968

You made a good start. You only have to get the actual value from the Request. The AllKeys returns an array of key names, iterate over it and get the value of it:

foreach(var key in Request.Form.AllKeys)
{
    var val = Request.Form[key]; // save it somewhere, process it
}

// Do something with values, like:
// insert item.Value into database (for example).

Upvotes: 4

Related Questions