Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Hi
I've seen numerous topics about people struggling to post an object to a controller, but none of them have really helped.
I've got a JSON object which has two arrays, one isn't populated so I expect it to be null (which it is):
"{"locationTypes":["Location Type"],"countries":[]}"
I'm using $.post to send this to the controller.
var getResults = function (selectedFilters, onSucess, onError) { $.post("/Umbraco/Api/Contact/GetResults", JSON.stringify(selectedFilters)).then(onSucess, onError); }
Controller method:
[System.Web.Http.HttpPost] public ContactPageViewModel GetResults(ContactSelection selectedFilters) { return new ContactPageViewModel(); }
ContactSelection object:
public class ContactSelection { public List<string> LocationTypes { get; set; } public List<string> Countries { get; set; } }
The controller is hit by the post, however selectedFilters' properties are both null.
Any help would be appreciated, thanks.
Alright just got it working. For some odd reason I need to use this implementation of post:
$.ajax({ type: "POST", url: "/umbraco/api/Contact/GetResults/", contentType: "application/json; charsect=utf-8", dataType: "json", data: JSON.stringify(selectedFilters) }).then(onSucess, onError)
I'd really like to know why though. If someone could answer that then I'll hand them a cookie.
You are able to use the $.post method.
If you specify your parameter name as "selectedFilters" in the Controller. You will have to set the variable name in the $.post function.
$.post("/Umbraco/Api/Contact/GetResults", { selectedFilters : JSON.stringify(selectedFilters) }, function(data) { console.log(data); });
Hope this works.
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Posting JSON to an API Controller
Hi
I've seen numerous topics about people struggling to post an object to a controller, but none of them have really helped.
I've got a JSON object which has two arrays, one isn't populated so I expect it to be null (which it is):
I'm using $.post to send this to the controller.
Controller method:
ContactSelection object:
The controller is hit by the post, however selectedFilters' properties are both null.
Any help would be appreciated, thanks.
Alright just got it working. For some odd reason I need to use this implementation of post:
I'd really like to know why though. If someone could answer that then I'll hand them a cookie.
You are able to use the $.post method.
If you specify your parameter name as "selectedFilters" in the Controller. You will have to set the variable name in the $.post function.
Hope this works.
is working on a reply...