ASP.net MVC - 使用DateTime的模型,尝试为jqGrid序列化Json() [英] ASP.net MVC - Model with DateTime, trying to serialize for Json() for jqGrid

查看:158
本文介绍了ASP.net MVC - 使用DateTime的模型,尝试为jqGrid序列化Json()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型...

    [DataContract]
    public class DeviceModel
    {
        [DataMember(Order=1)]
        public string Alias { get; set; }

        [DataMember(Order = 2)]
        public string Location { get; set; }

        [DataMember(Order = 3)]
        public string State { get; set; }

        [DataMember(Order = 4)]
        public DateTime? DateCreated { get; set; }

        [DataMember(Order = 5)]
        public string RatePlan { get; set; }

        public DeviceModel()
        {
            Alias = null;
            Location = null;
            State = null;
            DateCreated = null;
            RatePlan = null;
        }
    }

此模型包含一个DateTime对象,您可以看到。我使用这个模型作为我们使用的jqGrid插件的数据。唯一的问题是,当Grid加载时,DateCreated字段显示/ Date(1285128000000)/,而不是可读取的日期。我已经阅读了其他的一些帖子,但是我觉得他们不太适合我正在寻找的账单。我正在寻找一种将DateTime字段格式化为可读字符串的方法。建议?

This model contains a DateTime object, as you can see. I am using this model as the data for the jqGrid plugin we are using. The only issue is, the DateCreated field shows "/Date(1285128000000)/" when the Grid loads, instead of the readable date. I've read some of the other posts here, but I don't feel that they quite fit the bill for what I'm looking for. I'm looking for a way to format this DateTime field to a human readable string? Suggestions?

推荐答案

JSON只知道两个原始数据类型:字符串和数字,而 Date 类型。 Data Contract Serializer支持更多类型。例如 DateTime DateTimeOffset TimeSpan Guid Uri XmlQualifiedName 。如果您发送数据到客户端,也使用Data Contract Serializer来反序列化数据,您可以使用数据类型中的任何数据,而不会有任何问题。

JSON knows only two primitive datatypes: strings and numbers and no Date type. Data Contract Serializer supports more types. For example DateTime, DateTimeOffset, TimeSpan, Guid, Uri, XmlQualifiedName. If you send data to the client which use also Data Contract Serializer to deserialize the data you can use any from the datatypes without any problems.

您问题的最简单的解决方案在发送数据之前正在准备服务器上的数据。如果您仅序列化只有字符串和数字的对象作为属性或数组/ IList< T> ,那么您将没有问题。例如,默认情况下,jqGrid等待 ISO日期格式中的数据: Ymd ,数字为 Y m d 。如果您将服务器上的数据转换为Ymd格式,并在相应的 colModel 定义中使用 formatter:'date'您的问题将被解决。

The most simple solution of your problem is preparing the data on the server before sending the data. If you will serialize only objects which has only strings and numbers as properties or arrays/IList<T> then you will has no problem. For example, per default jqGrid wait the data in the ISO Date format: Y-m-d with numbers for Y, m and d. If you convert your data on the server to the Y-m-d format and use formatter:'date' in the corresponding colModel definition your problem will be solved.

您还可以使用自定义格式化程序 custom unformatter

这篇关于ASP.net MVC - 使用DateTime的模型,尝试为jqGrid序列化Json()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆