0

I'm trying to pass json data from view to controller but controller getting null all the time I tried every thing to resolve this issue but didn't find any solution here is my controller and json data

$("#ex_save").on("click",function() {
        var array = @Html.Raw(Json.Encode(Model));
        var json = JSON.stringify(array);
        $.ajax({
            type: "POST",
            url: "/Equipment/BulkUpdate",
            data: { jsonCollection : json },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function () { 
                console.log("Saved"); 
            },
            error: function (e) {
                console.log(e);
            }
        });
    });

this my ajax method to pass data to controller. and here is my controller to get that data :-

[HttpPost]
    public ActionResult BulkUpdate(string jsonCollection)
    {
        try
        {
            return View();
        }
        catch
        {
            throw;
        }
    }

here is the json what I'm passing enter image description here

here is the error :- enter image description here

5
  • Is there a specific reason you're sending JSON within JSON instead of just using the standard ModelBinder? Seems like you're just adding complexity for no reason except giving yourself more work. Commented Jul 3, 2017 at 9:35
  • @RoryMcCrossan I have editable table that I'm showing on view so user can edit it and press submit to save but I've pagination so it only pass first page data to controller collection so I tried this way. Commented Jul 3, 2017 at 9:38
  • You're passing a string and expecting a string, but your $.ajax call is telling it that it's actually json (when it's not, it's a string). Change the contentType to string or remove it. stackoverflow.com/a/18701357/2181514 Commented Jul 3, 2017 at 9:44
  • @RoryMcCrossan nope not worked still getting this error Invalid JSON primitive: jsonCollection. I changed dataType json to text Commented Jul 3, 2017 at 9:49
  • @RoryMcCrossan not getting value at controller but error is resolved. Commented Jul 3, 2017 at 10:07

2 Answers 2

1

You need to do this

$("#ex_save").on("click",function() {
        var array = @Html.Raw(Json.Encode(Model));
        var jsonData =  {jsonCollection : array};
        var postJson = JSON.stringify(jsonData);
        $.ajax({
            type: "POST",
            url: "/Equipment/BulkUpdate",
            data: postJson,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function () { 
                console.log("Saved"); 
            },
            error: function (e) {
                console.log(e);
            }
        });
    });

What you were doing is that you have used JSON.stringify() to convert the JSON in array variable and then you were assigning this to the new JSON object { jsonCollection : json } as POST request body. So it is a valid json object like {'name':'test1', 'age':'12'} then jQuery might not send it as json data but instead serialize it to name=test1&age=12 thus you get the error "Invalid JSON primitive: name"

And in your case, "Invalid JSON primitive: jsonCollection". So try using above code.

Sign up to request clarification or add additional context in comments.

4 Comments

it helped now not getting error but still not getting collection values at controller too.
But i think the problem here was to resolve the error. Now it is your side of workouts to check the response data or request payload in the server and debug.
I also think that it should be green marked as the answer solves the error.
can you please mark it green or if you need anything else from my side to improve the answer?
0
$("#ex_save").on("click",function() {
    var array = @Html.Raw(Json.Encode(Model));
    var jsonData =  {jsonCollection : array};
    var postJson = JSON.stringify(jsonData);
    $.ajax({
        type: "POST",
        url: "/Equipment/BulkUpdate",
        data: postJson,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function () { 
            console.log("Saved"); 
        },
        error: function (e) {
            console.log(e);
        }
    });
});

And instead of getting string at controller try to get ICollection

[HttpPost]
public ActionResult BulkUpdate(ICollection<your_class_type> jsonCollection)
{
    try
    {
        return View();
    }
    catch
    {
        throw;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.