如何使用模板t4获取属性类型以格式化jqgrid中的日期时间?

时间:2011-10-10 20:10:27

标签: c# .net-4.0 jqgrid t4

我正在尝试格式化jqgrid使用的日期。

我正在使用带有T4的MVC脚手架。

T4模板中有一部分像这样

jQuery("#ajaxGrid").jqGrid({
        url: '@Url.Action("GridData")',
        datatype: "json",
        jsonReader: { repeatitems: false, id: "<#= Model.PrimaryKeyName #>" },
        colNames: [<#= string.Join(", ", properties.Select(prop => "'" + prop.Name + "'")) #>],
        colModel: [
            <#= string.Join(", \r\n         ", properties.Select(prop =>
                string.Format("{{ name: '{0}', editable: true, sortable: true, hidden: {1}, align: {2} {3}}}", prop.Name, prop.Name == Model.PrimaryKeyName ? "true" : "false", "left", prop.Type.ToString() == "DateTime" ? "sorttype: 'date', datefmt: 'm/d/Y h:i AmPm'": "")
            )) #>
        ],
        rowNum: 5,
        pager: '#ajaxGridPager',
        width: '850',
        height: '15em'
    });

prop.Type.ToString()==“DateTime”? “sorttype:'date',datefmt:'m / d / Y h:i AmPm'”:“”不起作用。

我想格式化日期,如果该字段是DateTime,就像我看到的网页/日期(1315968717587)/我想更改T4模板,以便为将来的实体自动生成它。

我怎样才能做到这一点? 提前致谢!吉列尔莫。

修改

我读过我可以引用Microsoft.VisualBasic并使用

Microsoft.VisualBasic.Information.TypeName(prop)

但它正在返回CodeProperty2。我无法弄明白如何获得真正的类型。 我试过

Microsoft.VisualBasic.Information.TypeName(prop).Type 

然后它说: 错误CS1061:编译转换:'string'不包含'Type'的定义,也没有扩展方法'Type'接受第一个ar 可以找到'string'类型的gument(你是否缺少using指令或程序集引用?)

所以它知道它的字符串,但ToString()也返回CodeProperty2。

有什么想法吗? 提前致谢!吉列尔莫。

2 个答案:

答案 0 :(得分:0)

怎么样:

prop.Type.GetType() == typeof (System.DateTime) 
   ?  "sorttype: 'date', datefmt: 'm/d/Y h:i AmPm'"
   :  ""

答案 1 :(得分:0)

由于我还没有任何其他答案,我正在修理它,看着该物业的名称。如果它包含字符串“Date”,我将其格式化为Date,但我想避免这种情况,因为我不想绑定属性jqgrid的名称,我的意思是,应该能够命名属性无论什么人都想要,它应该继续工作并将其显示为日期。