简体   繁体   中英

How to pass string[] type parameters from jquery to asp.net web api method?

i wanna pass 3 parameters from jquery call to web api method :

string[] a, string[] b, string[] c. but i can not .

    <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# codes :

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

Add the string arrays to a new array like this:

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

Then deserialize the json data on the server side using the JavaScriptSerializer class :

//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'

You can use dynamic result = jsonparser.Deserialize<dynamic>(jsondata); if you don't have a class of it.
When you do, you can use MyClass result = jsonparser.Deserialize(jsondata,MyClass); to auto-assign the values using object initializers and return a new instance of the class that is filled with the values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM