开发者

How can I get the type of property with template t4 in order to format datetime in jqgrid?

开发者 https://www.devze.com 2023-04-12 14:52 出处:网络
I\'m trying to format the date used with jqgrid. I\'m using the MVC scaffolding with T4. There is one part in the T4 template like this

I'm trying to format the date used with jqgrid.

I'm using the MVC scaffolding with T4.

There is one part in the T4 template like this

jQuery("#ajaxGrid").jqGrid({
        url: '@Url.Action("GridData")',
        datatype: "json",
        jsonReader: { repeatitems: false, id: "<#= Model.PrimaryKeyNa开发者_开发技巧me #>" },
        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'": "" doesn't work.

I want to format the date if the field is DateTime as in the webpage I see /Date(1315968717587)/ and I want to change the T4 template to automatically generate it correctly for future entities.

How can I accomplish that? Thanks in advance! Guillermo.

EDIT

I've read that I can reference Microsoft.VisualBasic and use

Microsoft.VisualBasic.Information.TypeName(prop)

But it is returning CodeProperty2. I can't figure it out how to get the real type. I tried with

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

and then it says: error CS1061: Compiling transformation: 'string' does not contain a definition for 'Type' and no extension method 'Type' accepting a first ar gument of type 'string' could be found (are you missing a using directive or an assembly reference?)

So it knows it's string but ToString() returns CodeProperty2 as well.

Any ideas? Thanks in advance! Guillermo.


What about:

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


As I didn't have any other answer yet, I'm fixing it looking at the name of the property. If it contains the string "Date", I format it as Date, but I'd like to avoid that, since I don't want to tie the name of the property jqgrid, I mean, one should be able to name the property whatever one want and it should keep working and showing it as date.

0

精彩评论

暂无评论...
验证码 换一张
取 消