Ajax Datasource Data

4 posts, 1 answers
  1. Lennart
    Lennart avatar
    5 posts
    Member since:
    Jan 2013

    Posted 09 Jan 2013 Link to this post

    Hi,

    This question and example actually applies to the Grid but since it is about the DataSource I'm posting it in the General Area.

    As you can see below I'm using a read action for my Ajax request. And also using the Data method to provide the action with Data.

    When I only provide string or integer types in the return function (AdditionalData) everything is fine. But when I want to use a Date (i.e. StartDate, EndDate), the properties StartDate and EndDate are not passed to the Action Controller and the Action cannot be found (because the parameters do not match, I think).

    Is there a simple way to pass a Date object to your Action Controller or not to use the .Read(.. method but a jQuery ajax request?

    Kind regards,

    Lennart

    Grid Definition:
    @(Html.Kendo().Grid(Model) // Bind the grid to the Model property of the view
          .Name("Grid")
          .DataSource(dataSource => dataSource
            .Ajax()
            .Read(read => read.Action("HistoryDowntimes_Read", "TimeRegistration").Data("AdditionalData")))
          .Columns(columns =>
          {
              columns.Bound(p => p.TimeEventID);
      ....

    Data Return Function:
    function AdditionalData() {
            return {
                LocationID: 8,
                StartDate: new Date(2012, 1, 1, 11, 42, 30, 477),
                EndDate: new Date(2013, 1, 1, 11, 42, 30, 477)
            };
        }
    ==> Fixed LocationID and Date is for Development purposes only.
  2. Answer
    Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2172 posts

    Posted 11 Jan 2013 Link to this post

    Hi Lennart,

     
    Basically you should convert the Date object to string before sending it to the controller in order to be understand by the Default MVC Model Binder - for example using the "toGMTString" method. Please check the example below:

    AdditionalData function:

    function AdditionalData(e) {
        return {
            LocationID: 8,
            StartDate: (new Date(2012, 1, 1, 11, 42, 30, 477)).toGMTString(),
            EndDate: (new Date(2013, 1, 1, 11, 42, 30, 477)).toGMTString()
        };
    }

    Controller:
    public ActionResult Grid_Read([DataSourceRequest] DataSourceRequest request, DateTime? StartDate, DateTime? EndDate, int? LocationID)
    {
        ....
    Kind Regards,
    Vladimir Iliev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Lennart
    Lennart avatar
    5 posts
    Member since:
    Jan 2013

    Posted 11 Jan 2013 Link to this post

    Thanks Vladimir, this works!!!
  5. kunapa
    kunapa avatar
    2 posts
    Member since:
    Jan 2013

    Posted 08 Feb 2013 Link to this post

    I am getting error for this code:

    function AdditionalData(e) {
            return {
                FromDate: $("#FromDate").val().toGMTString(),
                ToDate: $("#ToDate").val().toGMTString()
            };
        }

    Error : Microsoft JScript runtime error: Object doesn't support property or method 'toGMTString'
Back to Top
UI for ASP.NET MVC is VS 2017 Ready