Blank Blank
Blank Blank

Reputation: 61

ajax post is returning null at the actionmethod

I am trying to retrieve the array of integers by posting it from ajax post as:

function myFunction() {
        var items = [];
        for(var i = 0; i<7;i++)
        {
         items[i]= i;
        }
        SubmitForm(items);
    }

    function SubmitForm(obj) {
        $.ajax({
            url: "/Home/Index",
            method: "POST",
            data: obj,
            success: function (data) {
                alert(data);
            }
            ,
            error: function (err) {
                console.log(err);
            }
        })
    }
<input type="submit" value="Submit" class="btn" onclick="myFunction();" />

My controller is as:

    Public JsonResult Index(int[] arr)
{
return View();
}

I have tried it with every parameter type but it still wont bind values to my action parameter. Do anyone knows what i am doing wrong?

Upvotes: 0

Views: 21

Answers (1)

JamesS
JamesS

Reputation: 2300

Try the following. You should try and pass the array as a JSON

function SubmitForm(obj) {
    $.ajax({
        url: "/Home/Index",
        type: "POST",
        data: JSON.stringify({"arr": obj}),
        contentType: 'Application/json',
        success: function (data) {
            alert(data);
        }
        ,
        error: function (err) {
            console.log(err);
        }
    })
}

Upvotes: 1

Related Questions