Reputation: 152
I'm facing a problem when calling an action on a controller with an argument
In the controller i have following action:
[HttpPost]
public ActionResult UpdateData(string month)
{
return Json(new { success = true, message = "Hello world" });
}
The ajax call in the view looks like:
$.ajax({
url: '@Url.Action("UpdateData")',
dataType: "json",
type: "POST",
contentType: 'application/json; charset=utf-8',
cache: false,
data: { },
success: function (data) {
if (data.success) {
alert(data.message);
}
},
error: function (xhr) {
alert(xhr.responseText);
}
});
this works well, but when i want to pass the argument like
data: {month:'may'}
i get an error, the action is never called. I just don't see it.
EDIT :
Ok, i just had to pass the json as a string:
data: "{month:'may'}"
I think i need more sleep :-)
Upvotes: 3
Views: 16189
Reputation: 14689
You have to just remove content type and also better if you pass controller name in URL:
$.ajax({
url: '@Url.Action("UpdateData","YourControllerName")',
dataType: "json",
type: "POST",
cache: false,
data: {month:'may'} ,
success: function (data) {
if (data.success) {
alert(data.message);
}
},
error: function (xhr) {
alert(xhr.responseText);
}
});
Upvotes: 3