POST json dictionary

后端 未结 10 889
猫巷女王i
猫巷女王i 2020-11-27 05:52

I\'m trying the following : A model with a dictionary inside send it on the first ajax request then take the result serialize it again and send it back to the controller.

10条回答
  •  爱一瞬间的悲伤
    2020-11-27 06:41

    Post the complex object as a string and deserialize at the other end. There is no type-safety however for this. Here is a dictionary with string key and string array values.

    js:

    var data = { 'dictionary': JSON.stringify({'A': ['a', 'b'] }) };
    
    $.ajax({
        url: '/Controller/MyAction',
        data: JSON.stringify(data),
        type: 'POST',
        contentType: 'application/json',
        dataType: 'json'
    });
    

    c# controller:

    [HttpPost]
    public ActionResult MyAction(string dictionary)
    {
        var s = new System.Web.Script.Serialization.JavaScriptSerializer();
        Dictionary d = s.Deserialize>(dictionary);
        return View();
    }
    

提交回复
热议问题