This is a migrated thread and some comments may be shown as answers.

How to pass grid data from 2 different grids into an ajax post in jquery

3 Answers 1266 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Toffer
Top achievements
Rank 2
Toffer asked on 18 Nov 2015, 11:56 PM

Hello all,

I wasn't sure where to put this as my grids are working...the problem is when I'm trying to pass the data from those grids into an ajax post...they keep coming up with an empty collection.  When I step through the jquery...the switches grid has 124 records and the goldList grid has over 1000, but when I hit the breakpoint in the controller the collections are empty. 

Here's the JQuery

function SwitchesModalCloseEvent(e)
{
    var switchesGrid = $("#SwitchModalGrid").data("kendoGrid");
    var goldListGrid = $("#ThirdPartyOpticsGoldListGrid").data("kendoGrid");
    var switchesGridObjects = switchesGrid._data; //.dataItems(); //.dataSource.data();
    var goldListGridObjects = goldListGrid._data;
 
    var goldListAddActionURL = '@Url.Action("AddToThirdPartyOpticsGoldList", "ThirdPartyOpticsGoldList")';
 
    $.ajax(
    {
        contentType: "application/json",
        data: { switchObjects: JSON.stringify(switchesGridObjects), goldListObjects: JSON.stringify(goldListGridObjects) },
        dataType: "json",
        type: "POST",
        url: goldListAddActionURL
    })
    .done(function () { alert('Website Called') })
    .error(function (objAjaxRequest, strError) {
        var respText = objAjaxRequest.responseText;
        console.log(respText);
    });
}

Here's the Controller Action

[HttpPost]
public ActionResult AddToThirdPartyOpticsGoldList([DataSourceRequest] DataSourceRequest request, IEnumerable<SwitchModel> switchObjects, IEnumerable<ThirdPartyOpticsGoldListModel> goldListObjects)
{
    TransceiverToSwitchReporting transceiverToSwitchReporting = new TransceiverToSwitchReporting(WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
    var thirdPartyOpticsGoldListResult = transceiverToSwitchReporting.AddToThirdPartyOpticsGoldList(switchObjects, goldListObjects);
 
    if (!thirdPartyOpticsGoldListResult.Result)
    {
        ModelState.AddModelError("AddToThirdPartyOpticsGoldList", thirdPartyOpticsGoldListResult.Message);
    }
 
    return Json(new[] { thirdPartyOpticsGoldListResult.Payload }.ToDataSourceResult(request, ModelState));
}

Any thoughts?

3 Answers, 1 is accepted

Sort by
0
Accepted
Kiril Nikolov
Telerik team
answered on 19 Nov 2015, 06:48 AM
Hello Toffer,

Every Grid has a dataSource instance, that takes care of the data. If you want to get the currently bound data to the grid, you need to use the dataSource.data() method as explained here:


http://docs.telerik.com/kendo-ui/api/javascript/data/datasource#methods-data

You can do this for both Grids and send the data with your Ajax call.

Regards,
Kiril Nikolov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Toffer
Top achievements
Rank 2
answered on 19 Nov 2015, 06:21 PM

So ultimately you were right so I had to mark it as answered...but really if I had been paying more attention to Fiddler I would have noticed it.  If you notice in my code snippets there is some commented out code where I'm creating my "switchesGridObjects" variable.  I had tried the .dataSource.data() method before...however I didn't notice that I got a different result when I attempted it.  What I missed was that I was getting a 500 response when using dataSource.data() with an error saying the json string was too big to parse.  I added the following to the <appSettings> section of my web.config and it solved the problem.  :)

        <add key="aspnet:MaxJsonDeserializerMembers" value="150000" />

Hopefully it helps someone else down the road.  Thanks for your nudge in the right direction Kiril!  :)


 
0
Kiril Nikolov
Telerik team
answered on 20 Nov 2015, 08:32 AM

Hello Toffer,

 

I am happy to hear that you managed to resolved the issue, and thanks for sharing your findings with the other users.

 

Have a great weekend!

 

Regards,
Kiril Nikolov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
General Discussions
Asked by
Toffer
Top achievements
Rank 2
Answers by
Kiril Nikolov
Telerik team
Toffer
Top achievements
Rank 2
Share this question
or