Making Datasource requests after leaving grid view

12 posts, 0 answers
  1. Jim
    Jim avatar
    18 posts
    Member since:
    Dec 2014

    Posted 26 Feb 2015 Link to this post

    I am using MVC5 and razor views with a kendo grid using ajax.  I generate a IEnumerable list of data for the grid.  When user hits button on grid the controller gets notified and opens up an edit view for the row that was clicked.

    What i want to be able to do is send data to that edit view so that it can move to next or previous record in the dataset that was in the grid.  I have achieved this by sending the id data from the displayed rows to the edit view via Viewbag.  I was able to learn how to send the viewsource data in the grid to the controller from this   thread

    however if you hit "next" and get to the record which was the last row in the original grid, i want to be able to execute my own datarequest to grab another page of data, intercept that data and just parse out the row id information to use for additional next/prev activities.

    i am stuck in figuring out how to make a new request on the original datasource now that i am not in the same view page.

    is this possible?
  2. Jim
    Jim avatar
    18 posts
    Member since:
    Dec 2014

    Posted 28 Feb 2015 in reply to Jim Link to this post

     I have a button in my grid view that when clicked fires back to the controller.  What i want to receive in the controller is the complete datarequest that generated the current grid's contents.  That way i can just modify the request and do something else with it.  However i do not know how to get the existing datarequest.  A new one is sent back to the controller, but it is always empty.

    I tried doing something like this:

    var data = JSON.stringify(this.dataSource.filter());
            $.ajax({
                type: "POST",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                url: "/Questions/RcvRows",
                data: data
            });

    And indeed i get a CompositeFilterDescriptor in my controller, but it too is empty.

    How can i get the existing request data out of the grid and back to my controller? 

    When the request is originally made, my controller's Read handler for the grid receives the request.  I tried to preserve that by saving it back via viewbag or viewdata but that didn't work either.  if that had worked i could pass the request data back to the controller through the viewbag when the button is pressed in the grid.

    any advice is appreciated.
    thx, jim
  3. Kendo UI is VS 2017 Ready
  4. Jim
    Jim avatar
    18 posts
    Member since:
    Dec 2014

    Posted 28 Feb 2015 in reply to Jim Link to this post

    Sorry for the double post.
  5. Jim
    Jim avatar
    18 posts
    Member since:
    Dec 2014

    Posted 28 Feb 2015 Link to this post

    just to be clear, posting the filter back to the controller wouldn't really get the job done, but i was trying to send datasource info back to the controller without any luck.
  6. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 03 Mar 2015 Link to this post

    Hi Jeff,

    In order to answer your question we need a better understanding of your implementation. Could you explain what "opens up an edit view" means? Do you open a new browser window? Or do you navigate to that edit view? Pasting the relevant code and grid declaration will also help.

    Regards,
    Atanas Korchev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  7. Jim
    Jim avatar
    18 posts
    Member since:
    Dec 2014

    Posted 03 Mar 2015 in reply to Atanas Korchev Link to this post

    ok, let me try to explain and boil it down.  I have a grid and it is filled with the results of a query (via ajax, using mvc5 and razor view engine).  

    Let's say the results of that query would give me rows with id's of 2,4,6,8,10,12,14,16,18,20.
    However the grid is only showing 5 rows per page, so the db query really only returned 2,4,6,8,10.

    Each row has an "EDIT" button that when clicked calls the controller action to open in a new edit view page, with the model item data of the row that was clicked.  fyi, I am using my own editing pages, not the grid edit in place functionality.

    When I edit the record for row id 4 (the second row in the grid) i am on new webpage different from the one that housed the grid view.  What i want to do on that new page is be able to know the last datasource request info from the grid prior to being directed to this new page.  From that new page, i can click a "Next" button which calls my controller "Next" action, and in that action i can use the old request info to advance the page and do a manual "ToDataSourceResult(request)" call against my db to pull another page of data to use in the controller action.

    Whenever the grid makes a call to the controller class, I can save the request information in a private static variable in my controller, and access that static from the "Next" handler in the controller.  It does work, but in a multi-user server farm environment where this app will live, using a static is not going to work, as it will get corrupted by multiple simultaneous users.

    I cannot figure out how to save the request info after i leave the grid page and pass it back to the controller when i call the "next" action from the "edit" page.

  8. Jim
    Jim avatar
    18 posts
    Member since:
    Dec 2014

    Posted 03 Mar 2015 in reply to Jim Link to this post

    I also tried saving the request information (DataSourceRequest object) as a field in my view model so when the ajax call into the controller (for getting new data for the grid), i take the request data and save it in each item in the enumerable model that i return to the view.  then when the "Edit" action calls into the controller, the model passed in will have the request data.

    this idea fails because the ".ToDataSourceResult(request)" cannot convert the DataSourceRequest object

    public ActionResult GridList([DataSourceRequest] DataSourceRequest request)
    {
    // Save this data request for future editing next/prev
    request = new DataSourceRequest
             {
                Aggregates = request.Aggregates,
                PageSize = request.PageSize,
                Filters = request.Filters,
                Groups = request.Groups,
                Page = request.Page,
                Sorts = request.Sorts
              },
              nSelected = -1
              };
        
    // populate my IEnumerable list of "questions", each adds the request object to each of the items in the list.
          var list = GetMyItems( request ).ToDataSourceResult( request );
          return Json( list );
       }
  9. Jim
    Jim avatar
    18 posts
    Member since:
    Dec 2014

    Posted 03 Mar 2015 in reply to Jim Link to this post

    the error is:  Additional information: Unable to process the type 'Kendo.Mvc.SortDescriptor[]', because it has no known mapping to the value layer.

    i can't convert the collections inside datasourcerequest into json.
  10. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 05 Mar 2015 Link to this post

    Hello Jeff,

    You can try saving and restoring the grid state. When the user clicks the edit button save the state to local storage or a cookie. When the user goes back to the grid page load the state (if available).

    Regards,
    Atanas Korchev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  11. Jim
    Jim avatar
    18 posts
    Member since:
    Dec 2014

    Posted 05 Mar 2015 in reply to Atanas Korchev Link to this post

    Hi Atanas, thanks for the idea, but i think you may have missed the point of my question.
    I want the Datasource's last DataSourceRequest object to be passed onto the Edit view so when it posts to the controller, the controller can access that request, make some updates and then excercise the request against the db.

    1.  The datasourcerequest gets sent from grid to controller to show return one page of records for grid.
    2.  grid shows records.
    3.  user clicks a grid record to go to a new edit view.  That is done via a call to the controller action.  
    4.  I want that call to the controller action to be able to get the datasourcerequest object.
    5.  the datasourcerequest object is then sent to the new edit view.
    6.  when the edit view posts its form back to controller, i want the datasourcerequest object posted back with the form data to the controller's handler that handles edit view posts.

  12. Jim
    Jim avatar
    18 posts
    Member since:
    Dec 2014

    Posted 05 Mar 2015 Link to this post

    I have the same question as the poster in this thread.

    The answer was to serialize the datasource.view() and send that back to the controller via ajax.
    I don't want the DataSource.view() info, i want the DataSourceRequest object that generated the grid display....not just the results of that request.

    I don't know how to get the request and serialize it back to the controller in this way.

    I want this because i want the next view rendered by the controller to have access to the prior request so i can use that request to do my own gathering of a new page of data from the database, using all the same filters, sorting and aggregation info that went into the previous DataSourceRequest.

  13. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 06 Mar 2015 Link to this post

    Hello Jeff,

    In that case I will recommend the approach used in this project. It uses the parameter map of the ASP.NET MVC transport which knows how to encode the current data source state in a format which DataSourceRequest can parse.

    Regards,
    Atanas Korchev
    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