简体   繁体   English

控制器返回错误的DateTime格式

[英]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 在我的控制器中使用Telerik DataSourceRequest时,将返回具有DateTime数据类型的任何属性,如下所示:

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

Instead of MM/dd/yy 代替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. 我还在文件中包含了Telerik的js文化参考,并且没有运气就启动了kendo.culture() 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);
        }
    }

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. 顺便说一句,您将需要Newtonsoft.Json才能使用JsonConvert。

Thanks for all the responses. 感谢您的所有回复。 The resolution on this was to include some client side JavaScript which formats the field at runtime: 解决方案是添加一些客户端JavaScript,以在运行时格式化字段:

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: 然后将一个添加到Telerik Grid列:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM