2009-09-29 66 views
5

我想吐出Request.Form中的所有東西,這樣我就可以將它作爲字符串返回並查看我正在處理的內容。我試圖建立一個for循環...如何在不知道任何細節的情況下遍歷Request.Form?

// 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; 
} 

但所有我曾經得到的回覆是「12」,我知道有很多更多的東西的形式比上...

回答

13
StringBuilder s = new StringBuilder(); 
foreach (string key in Request.Form.Keys) 
{ 
    s.AppendLine(key + ": " + Request.Form[key]); 
} 
string formData = s.ToString(); 
10
foreach(string key in Request.Form.Keys) 
{ 
    posted += Request.Form[key].ToString(); 
} 
+0

+1就是這樣:) – 2009-09-29 15:14:06

0
foreach(KeyValuePair<string, string> kvp in Request.Form){ 
    posted += kvp.Key + ":" + kvp.Value + "\n"; 
} 

編輯:哦。顯然你必須hack the NameValueCollection才能做到這一點。所以這是迭代集合的一種不好的方法。

+0

指定的轉換無效。 – BigOmega 2009-09-29 15:18:20

3

OHHH我想出了我的問題,在我的形式中,我一直在獲取的一個值來自唯一具有NAME的輸入控件。現在,我給他們的名字,它正在工作。

相關問題