posting JSON to MVC controller - String parameter is null

前端 未结 1 2007
心在旅途
心在旅途 2021-01-23 16:22

I am intentionally trying NOT to use a binding in the controller parameter, so I have a controller that looks like:

 [HttpPost]
        public ActionResult Untyp         


        
相关标签:
1条回答
  • 2021-01-23 16:45

    The function serializeArray creates a Javascript object with the form's key/value pairs. You don't want that, you want a single key/value with serializedformdata = (JSON string). Ie, like this;

    var formelements = { serializedformdata: JSON.stringify($('#form').serializeArray()) };
    

    This passes the raw JSON string to the controller's parameter. You can use the JavaScriptSerializer to get the object on the server:

    var obj = (List<Dictionary<string,string>>)new JavaScriptSerializer().Deserialize(serializedformdata, typeof(List<Dictionary<string,string>>));
    Dictionary<string,string> dict = obj.First();
    string someval = dict["somekey"];
    
    0 讨论(0)
提交回复
热议问题