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

Ajax Datasource Data

3 Answers 108 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Lennart
Top achievements
Rank 1
Lennart asked on 09 Jan 2013, 09:49 AM
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.

3 Answers, 1 is accepted

Sort by
0
Accepted
Vladimir Iliev
Telerik team
answered on 11 Jan 2013, 08:39 AM
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!
0
Lennart
Top achievements
Rank 1
answered on 11 Jan 2013, 08:53 AM
Thanks Vladimir, this works!!!
0
kunapa
Top achievements
Rank 1
answered on 08 Feb 2013, 04:48 PM
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'
Tags
General Discussions
Asked by
Lennart
Top achievements
Rank 1
Answers by
Vladimir Iliev
Telerik team
Lennart
Top achievements
Rank 1
kunapa
Top achievements
Rank 1
Share this question
or