2015-03-31 19 views
0

我想了解窗體和查詢字符串中的GetValues在context.RequestContext.Request的GetValues和多種選擇的元素

我有這樣一個URL,從Ajax處理多選擇HTML元素生成

generic.ashx?tags=1&tags=2 

理論上不應該這樣(通過郵件提交)將字符串標籤設置爲1,2?

string[] tags = context.Request.Form.GetValues("tags"); 

我已經使用GET方法也試過,使用查詢字符串或者只是上下文請求,沒有到目前爲止

string[] tags = context.Request.QueryString.GetValues("tags"); 
string[] tags = context.Request.GetValues("tags"); 

我的底線是我想建立一個SQL WHERE子句

int tagscount = tags.Count(); 

     string sWhere =""; 

     if (tagscount != 0) { 
      sWhere ="Where ("; 
      for (int i = 0; i < tagscount; i++) 

      { 

       sWhere += " tag_id ="+tags[i]+")"; 
       if (i < tagscount -1){ 
        sWhere += " OR "; 
       } 

       } 
       sWhere += ")"; 
      } 

但老實說我只是很高興在這一點上顯示我的字符串正在填充

results = string.Format("{{ \"tags\": {0} }}",tags); 
context.Response.Write(results); 
+0

您確定這不會使用您期望的兩個值填充數組嗎? var tags = context.Request.QueryString.GetValues(「tags」); – 2015-03-31 15:31:22

回答

1

GetValues()應該可以工作,而不是嚴格地將值傳遞給string [],試着使用var並迭代以從中獲取每個值。

var values = context.Request.QueryString.GetValues("tags"); 
foreach (var item in values) 
{ 
    //do your thing 
}