I built a half working ajax code using jQuery and JSON, but I can't get data parameters from the POST request, although I tried to send it in a few different ways (first as an object inside data: {} object, then as just string), but nothing worked. Here is the code:
C#, ManageController.cs:
public ActionResult SubmitForm(string typeAction)
{
string message = (int.Parse(typeAction) * 20).ToString();//Exception: can't convert null string to int
return Json(new {Message = message, JsonRequestBehavior.AllowGet});
}
JavaScript (+jQuery, of course), AppScripts.js:
function AjaxPost(typeofAction, ActionUrl) {
$.ajax({
type: "POST",
url: ActionUrl,
data: { typeAction: JSON.stringify(typeofAction) },
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function () {
return true;
},
error: function (xhr, textStatus, errorThrown) {
console.error(textStatus + "\n" + errorThrown);
}
});
return false;
}
which called from button:
<button onclick="AjaxPost($('#SimpleActionId').value, '/manage/SubmitForm')">Go!</button>
for the results, the action IS called and I can see it executed by the debugger, until exception is thrown because can't convert null string to int. the parameter doesn't even gets into the ActionResult SubmitForm, but it's called, and all the values are sent from the data.
thanks.