0

As part of validation I am trying to use a list of values from a dropdown box on the form. When creating a variable like this var form = HttpContext.Current.Request.Form, I can see all the values from the form. In Visual Studio's debugger, the list I'm interested in shows as an array of Text/Value pairs. How can I extract the Values from the form?

1 Answer 1

2

Form is already a collection of "Text/Value" pairs. Its type is NameValueCollection, and it stores "Name/Value" pairs. The debugger doesn't make it super easy to navigate since it doesn't show you the values unless you index into it. However, it's very easy to enumerate:

foreach (string key in form.AllKeys)
{
     Console.WriteLine("{0} - {1}", key, form[key]);
}

If you don't like not being able to see the values in the debugger windows, you could copy the name/value pairs to a dictionary of key/value pairs first, then inspect the dictionary:

var dictionary = new Dictionary<string, object>();
HttpContext.Current.Request.Form.CopyTo(dictionary);

You can access values by key or iterate this in the same way as above, although there's really no value in converting the original collection to a dictionary if your only gripe is how the debugger displays its contents.

Sign up to request clarification or add additional context in comments.

5 Comments

This is nice but I'm trying to extract a specific array of values. In the dictionary I have an array of values like this MyFormValue[0].Text MyFormValue[0].Value. How can I extract all the Values from the MyFormValue array?
I think I would need to first get a count and then simply loop through them?
You could get an array (List<>) of just the values with form.AllKeys.Select(k => form[k]).ToList(), or if you're taking the Dictionary approach, dictionary.Values.ToList();.
This still gives me all values from the form rather than just the values from the specific array I need. I only need values from MyFormValue array
It's really difficult to understand exactly what you're asking for. Please edit your question to include the HTML for your form, and the expected values you want from the form, to be pulled from the Form object.

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.