A.naam
A.naam

Reputation: 405

ASP.NET MVC Ajax JSON jQuery won't send parameters to `ActionResult`

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.

Upvotes: 0

Views: 291

Answers (2)

Henrik Erlandsson
Henrik Erlandsson

Reputation: 3830

IActionResult implementation passes some data types correctly but all strings as null.

I've isolated the error to this:

contentType: "application/json; charset=utf-8",

If omitted, it is set to the $.ajax default, and strings will arrive as strings to your action method.

contentType: application/x-www-form-urlencoded; charset=UTF-8,

Upvotes: 0

Kunal Mukherjee
Kunal Mukherjee

Reputation: 5853

1) Modify your AJAX call to look like this:

function AjaxPost(typeofAction, ActionUrl) {    
    $.ajax({
        type: "POST",
        url: ActionUrl,
        data: JSON.stringify({ typeAction: typeofAction }),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            console.log(data);
            return true;
        },
        error: function (xhr, textStatus, errorThrown) {
            console.error(textStatus + "\n" + errorThrown);
        }
    });
    return false;
}

2) Make a C# model:

public class ActionType
{
    public string typeAction { get; set; }
}

3) Change your action method to look like:

public ActionResult Edit(ActionType RequestModel)
{
    string message = (int.Parse(RequestModel.typeAction) * 20).ToString();
    return Json(new  {Message = message }, JsonRequestBehavior.AllowGet); 
}

Now appropriate model binding will take place and you will get your typeAction into the RequestModel.typeAction property.

Upvotes: 2

Related Questions