How to traverse through Request.Form without knowing any details?

MetaMagnate picture MetaMagnate · Sep 29, 2009 · Viewed 13.1k times · Source

I want to spit out everything in Request.Form so I can just return it as a string and see what I am dealing with. I tried setting up a for loop...

// Order/Process
// this action is the submit POST from the pricing options selection page
// it consumes the pricing options, creates a new order in the database,
// and passes the user off to the Edit view for payment information collection

[AcceptVerbs(HttpVerbs.Post)]
public string Process()
{
    string posted = "";
    for(int n = 0;n < Request.Form.Count;n++)
        posted += Request.Form[n].ToString();
    return posted;
}

But all I ever get back is '12' and I know there are a lot more things on the form than that...

Answer

D&#39;Arcy Rittich picture D'Arcy Rittich · Sep 29, 2009
StringBuilder s = new StringBuilder();
foreach (string key in Request.Form.Keys)
{
    s.AppendLine(key + ": " + Request.Form[key]);
}
string formData = s.ToString();