sdtechguy
sdtechguy

Reputation: 113

Controller Returning Incorrect DateTime Format

When using the the Telerik DataSourceRequest within my controller any property with a DateTime data type is being returned as

{"Data":[{"EffectiveStart":"\/Date(1393660800000)\/"}

Instead of MM/dd/yy

The property on my Model is:

    [DataType(DataType.DateTime)]
    public DateTime EffectiveStart;

I have also included the js culture reference for Telerik in my file and initiated kendo.culture() with no luck. What am I missing?

As requested here is the controller:

    public ActionResult Grid_Read([DataSourceRequest]DataSourceRequest request, int id)
    {
        try
        {
            using (var db = new MyEntities())
            {

                var query = from refA in db.Entity
                            join refB in db.Entity on refA.ID equals refB.ID
                            where refA.ID == id
                            select new ResultList
                            {
                                ResultId = refA.PayeeId,
                                EffectiveStart = refA.EffectiveStart,
                            };

                List<ResultList> myvar = query.ToList();

                DataSourceResult result = myvar.ToDataSourceResult(request);

                return Json(result);
            }

        }
        catch (Exception ex)
        {
            return Json(null);
        }
    }

Upvotes: 2

Views: 1151

Answers (2)

sdtechguy
sdtechguy

Reputation: 113

Thanks for all the responses. The resolution on this was to include some client side JavaScript which formats the field at runtime:

function toDate(value)
    var dateRegExp = /^\/Date\((.*?)\)\/$/;
    var date = dateRegExp.exec(value);
    return new Date(parseInt(date[1]));
}

Then add a to the Telerik Grid column:

.ClientTemplate("#= kendo.toString( toDate(DateCreated), \"MM/dd/yyyy\" ) #")

Upvotes: 1

nepomucenobr
nepomucenobr

Reputation: 278

One thing you could do is replace

return Json(result);

by

return Content(JsonConvert.SerializeObject(result));

BTW, you will need Newtonsoft.Json to use JsonConvert.

Upvotes: 2

Related Questions