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

Abhay
  • 47
  • 1
  • 10
  • 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. – Rory McCrossan Jul 03 '17 at 09: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. – Abhay Jul 03 '17 at 09: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. https://stackoverflow.com/a/18701357/2181514 – freedomn-m Jul 03 '17 at 09:44
  • @RoryMcCrossan nope not worked still getting this error _Invalid JSON primitive: jsonCollection._ I changed dataType _json_ to _text_ – Abhay Jul 03 '17 at 09:49
  • @RoryMcCrossan not getting value at controller but error is resolved. – Abhay Jul 03 '17 at 10:07

2 Answers2

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.

Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
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;
    }
}
Abhay
  • 47
  • 1
  • 10