Posting a JSON string for an ASP.NET MVC 3 action results in a null parameter

I am sending a JSON string to asp.net MVC as follows.

Ajax call

$.ajax({ type: "POST", url: "@(storeLocation)IDR/OpcInsertCustomerProfile/", data: JSON.stringify(currSelection), contentType: "application/json", success: function(data) { alert('success : ' + JSON.stringify(data)); }, error: function(data) { alert('Error : ' + JSON.stringify(data)); } } ); 

In the controller:

 [HttpPost] [ActionName("OpcInsertCustomerProfile")] public JsonResult OpcInsertCustomerProfile(string currSelectionData) { try { JavaScriptSerializer ser = new JavaScriptSerializer(); var res = ser.Serialize(currSelectionData); return Json(currSelectionData, JsonRequestBehavior.AllowGet); } catch (Exception exc) { return Json(new { error = 1, message = exc.Message }); } } 

The debugger indicates that the action was successfully called, however, the received parameter of the incoming line is always zero. Firebug 'post' shows that the outgoing parameter is a valid json object. I expect to see a JSON string as an input parameter. Note that I do not want to de-serialize it to the corresponding object. All I want to do is save the string in JSON 'as-it-is' format in the database. Later it needs to get the ans passed in Javascript as it is.

+4
source share
5 answers

Try the following:

 $.ajax({ type: "POST", url: "@(storeLocation)IDR/OpcInsertCustomerProfile/", data: { "currSelectionData" : "'" + JSON.stringify(currSelection) + "'" }, contentType: "application/json", success: function(data) { alert('success : ' + JSON.stringify(data)); }, error: function(data) { alert('Error : ' + JSON.stringify(data)); } } ); 
+3
source

One way is to allow the action to receive the parameter as POST data instead of the "Stringified" JSON data. To do this, send the data without JSON.Stringify. Hope this is what you need.

If not, you can try to create an object only to get this simple data.

+1
source

Look at your action method: does this method correctly accept a string with serialized JSON, than serialized that string again as JSON, then canceled the result and serialized and returned the same string again?

If you send a request using an / json application such as ASP.NET MVC tries to deserialize the resulting string and bind it to the action parameters: in your case, it tries to find the currSelectionData property inside your JSON object. Does this property exist? Maybe you expect the whole string to be retrieved as the currSelectionData parameter? Then you need to use FormCollection or Request.Form, because the middleware does not support this by default.

0
source

In fact, I am filling out a dropdown list of states in the json country selection. I like it

I have an action in my controller, it returns my data in json format, as below

 public JsonResult State(int countryId) { var stateList = CityRepository.GetList(countryId); return Json(stateList, JsonRequestBehavior.AllowGet); } 

in my opinion

 <script type="text/javascript"> function cascadingdropdown() { $("#stateID").empty(); $("#stateID").append("<option value='0'>--Select State--</option>"); var countryID = $('#countryID').val(); $.ajax({ url: "/City/State", dataType: 'json', data: { countryId: countryID }, success: function (data) { $("#stateID").empty(); $("#stateID").append("<option value='0'>--Select State--</option>"); $.each(data, function (index, optiondata) { alert(optiondata.StateName); $("#stateID").append("<option value='" + optiondata.ID + "'>" + optiondata.StateName + "</option>"); }); }, error: function () { alert('Faild To Retrieve states.'); } }); } </script> 

I think this will help you ...

0
source

When sending json via Ajax, I think this is the right approach for the data property in Ajax: data: "{'parameterNameInAction':'" + JSON.stringify(jsonData) + "'}"

0
source

Source: https://habr.com/ru/post/1436519/


All Articles