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

Setting a date in a DataItem from a DataSource using the "set" method

2 Answers 352 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Toby
Top achievements
Rank 1
Toby asked on 09 Jun 2016, 12:44 PM

I'm setting the value of a property using the following JavaScript code

var documentList = $("#documentsList").data("kendoListView");
var dataSource = documentList.dataSource;
var dataUid = documentList.select().attr("data-uid");
var item = dataSource.getByUid(dataUid);
var datepicker = $("#documentDate").data("kendoDatePicker");
 
item.set("DocumentDate", datepicker.value());
 
if (dataSource.hasChanges()) {
  dataSource.sync();
}

My controller is receiving the post when the datasource is synced and the other properties are being set. This is the C# of my controller

public JsonResult Update([DataSourceRequest]DataSourceRequest request, InboxDocument document)
{
   return Json(new[] {SharePointHelper.UpdateInboxDocument(HttpContext, document)}.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
 
}

All the other properties that are set are being sent through, but when I look at the request sent through in F12 tools, the following is displayed.

{DocumentDate: {errors: ["The value '6/21/2016 1:00:00 AM' is not valid for DocumentDate."]},…}

This is the same for any other date values.

 

 

2 Answers, 1 is accepted

Sort by
0
Stephen
Top achievements
Rank 2
answered on 10 Jun 2016, 02:29 PM

If I remember correctly from the multiple time I've had to fight with dates....

I think the problem is that datepicker.value() returns a string, not a date object.

Then when you do the .set(), it is simply treated as a string and the model value is now set to a string...not a date anymore and so it does not get serialized as a date when sending to the server and the MVC model binder is unable to bind the string value to the DateTime field, probably due to not being able to parse that particular string format to a date.

Try wrapping the datepicker.value() in a kendo.parseDate(), i.e.

item.set("DocumentDate", kendo.parseDate(datepicker.value()));

and see if that helps as this will set the the model value to an actual date object instead of "overwriting" the datatype to a string. 

 

 

0
Daniel
Telerik team
answered on 13 Jun 2016, 08:56 AM
Hi,

The value method should always return a Date object or null and parseDate will automatically be called by the model if the field is declared as date which should also happen automatically when using the wrappers. The most likely reason for the problem is that the client-side and server side cultures are not the same in which case the dataSource will not serialize the dates as expected on the server.

Regards,
Daniel
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Toby
Top achievements
Rank 1
Answers by
Stephen
Top achievements
Rank 2
Daniel
Telerik team
Share this question
or