MVC Json DateTime handling

When working on a page where we are displaying dates in grids I ran into a properly displaying dates in the right format.  This is a problem with how MVC JSONResults handles dates.   We had solved this problem in the past and I wanted to write it up for future reference.   Instead of using JsonResult we need to use the Newtonsoft.Json JsonNetResult type to pass the data to the Clientside.

using Newtonsoft.Json;
public JsonNetResult GetDFLessonsCompletedData(string programId, string periodValue)
{
var parameters = SetProgramParameters(programId, String.Format("Problem downloading Lesson Completed for ProgramID:{0} and Period:{1}", programId, periodValue));
var period = (TimePeriod)Enum.Parse(typeof(TimePeriod), periodValue);
parameters = SetTimePeriod(period, parameters);
var data = DFLessonCompletedResource.GetDetails(parameters);
// use ISO 8601 date formats
var jsonNetResult = new JsonNetResult {Formatting = Formatting.Indented, Data = new AjaxDataResult(data)};
return jsonNetResult;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s