ParamQuery grid support forum
General Category => ParamQuery Pro Evaluation Support => Topic started by: daan on September 29, 2014, 12:43:36 pm
-
Found another problem that I haven't been able to solve yet.
I have a list of objects that contains
public DateTime StartDate { get; set; }
Then I used your example code to the list back from MVC
StringBuilder sb = new StringBuilder(@"{""totalRecords"":" + total_Records + @",""curPage"":" + pq_curPage + @",""data"":");
JavaScriptSerializer js = new JavaScriptSerializer();
String json = js.Serialize(orders);
sb.Append(json);
sb.Append("}");
return this.Content(sb.ToString(), "text/text");
In the grid I added a column with these settings
title: "Start Date", minWidth: "190", dataIndx: "StartDate", dataType: "date"
When I view the page, instead of getting the date I get the serialized version
/Date(1288566000000)/
Any idea why? How can get the deserialized date?
-
you would need to implement custom json converter.
public class invoiceConvertor : JavaScriptConverter
{
//private const string _dateFormat = "MM/dd/yyyy";
private const string _dateFormat = "yyyy-MM-dd";
public override IEnumerable<Type> SupportedTypes
{
get
{
return new[] { typeof(Invoice) };
}
}
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
//we are not using Deserialize for this example.
return new Object();
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
IDictionary<string, object> serialized = new Dictionary<string, object>();
var order = (Invoice)obj;
foreach (var prop in order.GetType().GetProperties())
{
if (prop.PropertyType == typeof(DateTime?))
{
DateTime? val = (DateTime?)prop.GetValue(order, null);
serialized[prop.Name] = (val == null) ? "" : ((DateTime)val).ToString(_dateFormat);
}
else
{
serialized[prop.Name] = prop.GetValue(order, null);
}
}
return serialized;
}
}
JavaScriptSerializer js = new JavaScriptSerializer();
js.RegisterConverters(new[] { new invoiceConvertor() });
-
That worked! Thanks!
Is this the only way to do it? Lets say if we have a huge system with hundreds of grids connected to hundreds of classes, we would have to do a custom converter for every class? I've used other grids in the past, but never encountered this behavior before. Still, I like this grid and it's not a breaking point, just would be great to have something like this built into the grid :D
-
This is not the only way.
You can also do it client side in dataModel.getData callback, iterate over the data and convert all data where column.dataType=="date"
-
yet another way to do it on the server side is by use of JSON.NET serializer.
http://james.newtonking.com/archive/2009/02/20/good-date-times-with-json-net