2016-05-20 6 views
0

jquery呼び出しからweb APIメソッドに3つのパラメータを渡します。jqueryからasp.net Web APIメソッドに文字列[]型のパラメータを渡すには?

文字列[] a、文字列[] b、文字列[] c。でも、私は出来ません 。

<script type="text/javascript"> 

    function fncsave() { 

     var arrtemplate = []; 
     var arrrole = []; 
     var arrpcode = []; 
     $('#mytemplateTags li').each(function() { 
      var str = $(this).html(); 
      var res = str.match("<span class=\"tagit-label\">(.*?)</span>"); 
      if (res!=null) { 
       var str = res[1]; 

       arrtemplate.push(str); 
      } 
     }); 

     $('#myroleTags li').each(function() { 
      var str = $(this).html(); 
      var res = str.match("<span class=\"tagit-label\">(.*?)</span>"); 
      if (res != null) { 
       var str = res[1]; 

       arrrole.push(str); 
      } 
     }); 
     $('#myprocessCodeTags li').each(function() { 
      var str = $(this).html(); 
      var res = str.match("<span class=\"tagit-label\">(.*?)</span>"); 
      if (res != null) { 
       var str = res[1]; 

       arrpcode.push(str); 
      } 
     }); 



     console.log(JSON.stringify(arrtemplate)); 
     $.ajax({ 
      url: "/api/TagCloud/SessionTemplate", 
      method: "Post", 
      data:JSON.stringify({ templates : arrtemplate, roles : arrrole, pcodes : arrpcode}), 
      async: false, 
      contentType: 'application/json; charset=utf-8', 
      dataType: "json", 
      success: function (msg) { 
       console.log(msg); 
       if (msg == true) { 

        // alert("true"); 
       } 
      } 
     }); 




    } 

     </script> 

のC#コード:

[HttpPost] 
    public bool SessionTemplate(string[] templates, string[] roles, string[] pcodes) 
    { 
     HttpContext.Current.Session["templates"] = templates; 
     HttpContext.Current.Session["roles"] = roles; 
     HttpContext.Current.Session["pcodes"] = pcodes; 
     return true; 
    } 
+0

'string []'をメンバとするモデル/クラスを定義し、JSONを送信します。 AFAIKでは、複数のパラメータをWeb API POSTメソッドに渡すことはできません。 –

+0

何が間違っていますか?どの部分に問題がありますか? – x13

+1

あなたはあなたのconsole.log(JSON.stringify({template:arrtemplate、roles:arrrole、pcodes:arrpcode}))を投稿できますか? –

答えて

1

このような新たな配列に文字列配列を追加:

//the nested array (hardcoded for clarity) 
var newarray = {a:['a','b','c'],b:['d','e','f']a:['g','h','i']}; 
var jsondata = JSON.stringify(newarray); 
//send jsondata to server 

そしてJavaScriptSerializer classを使用して、サーバー側のJSONデータをデシリアライズ:

//assign submitted json to the variable jsondata (hardcoded for clarity) 
String jsondata = "{a:['a','b','c'],b:['d','e','f']a:['g','h','i']}"; 
JavaScriptSerializer jsonparser = new JavaScriptSerializer(); 
dynamic result = jsonparser.Deserialize<dynamic>(jsondata); 
Respone.Write(result['a',2]); //writes 'c' 

クラスがない場合はdynamic result = jsonparser.Deserialize<dynamic>(jsondata);を使用できます。
MyClass result = jsonparser.Deserialize(jsondata,MyClass);を使用すると、オブジェクト初期化子を使用して値を自動割り当てし、値でいっぱいになったクラスの新しいインスタンスを返すことができます。

関連する問題