Post array of strings to web API method

前端 未结 7 1321
伪装坚强ぢ
伪装坚强ぢ 2021-02-14 14:42

this is my client side ajax call:

    var list = [\"a\", \"b\", \"c\", \"d\"];

    var jsonText = { data: list };

    $.ajax({
        type: \         


        
7条回答
  •  既然无缘
    2021-02-14 15:14

    use the method above to send array as suggested by cris in your jquery ajax call. JSON data is usually in key value pair.

      var tmp = [];
      tmp.push({
      //this is the key name 'a'  "a": "your value", //it could be anything here in 
       //string format.
        "b": "b",
        "c": "c",
        "d": "d"
       });
    
     { data: JSON.stringify(tmp);} 
    

    You can also accomplish above by using two dimensional array

    Additionally do this in the webapi project.

    under the models folder in your web api project create a class file. Possibly class1.cs.

    Create 4 properties

    public string a {get; set;}
    public string b {get; set;}
    public string c {get; set;}
    public string d {get; set;}
    

    Now do this in your controller

    using projectname.models
    
    [HttpPost]
    public return-type actionname(List obj)
    {
      //Further logic goes here
    } 
    

    I am sure this will work.

提交回复
热议问题