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

4 posts, 1 answers
  1. Toffer
    Toffer avatar
    34 posts
    Member since:
    Sep 2015

    Posted 18 Nov 2015 Link to this post

    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?

  2. Answer
    Kiril Nikolov
    Admin
    Kiril Nikolov avatar
    2565 posts

    Posted 19 Nov 2015 Link to this post

    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!
     
  3. Kendo UI is VS 2017 Ready
  4. Toffer
    Toffer avatar
    34 posts
    Member since:
    Sep 2015

    Posted 19 Nov 2015 in reply to Kiril Nikolov Link to this post

    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!  :)


     
  5. Kiril Nikolov
    Admin
    Kiril Nikolov avatar
    2565 posts

    Posted 20 Nov 2015 Link to this post

    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!
     
Back to Top
Kendo UI is VS 2017 Ready