2011-09-06 16 views
14

Tengo una cuadrícula web y no estoy usando razor syntex.Así que estoy usando el formulario .aspx. el código está debajo;Cómo dar formato a la fecha en la cuadrícula web de mvc

<% 
    var grid = new WebGrid(Model,defaultSort:"PublishDate",rowsPerPage:10); 
    %> 
    <%: 
     grid.GetHtml(
          tableStyle: "wGrid", 
          headerStyle: "wGridHeader", 
        alternatingRowStyle: "alt", 
        columns: grid.Columns(
        grid.Column("Title", canSort: false), 
        grid.Column("PublishDate", "Published on"), 
        grid.Column("CategoryName", "Category"), 
        grid.Column(format: (item) => Html.ActionLink("Details", "Browse", new { id = item.Title })) 
       ) 
       ) 
    %> 

Ahora quiero dar formato a la columna 'PublishDate' a algo así como 'dd-mmm-aaaa'. ¿Alguna idea de cómo hacer esto?

Respuesta

34
grid.Column(
    "PublishDate", 
    "Published on", 
    format: (item) => string.Format("{0:dd-MMM-yyyy}", item.PublishDate) 
) 
+0

:) cool..it era tio por lo simple..thanks. – kandroid

5

Esto funciona para mí, y permite valores nulos

grid.Column("End_Date",format: item => ((item.End_Date == null) ? "" : item.End_Date.ToString("MM/dd/yyyy"))), 
+0

esto funciona muy bien si tiene valores nulos. – Crismogram

10

Si DateTime propiedad se define como (puede contener nulos):

public DateTime? WorkedDate { get; set; } 

Usar este formato:

grid.Column("WorkedDate", "Last Worked On", 
    format: (item) => item.WorkedDate != null 
    ? item.WorkedDate.ToString("MM/dd/yy") : "", canSort: true) 

De lo contrario, si se define como bel ow (no puede ser nulo), tendrá la fecha actual o .MinDate como valor predeterminado.

public DateTime WorkedDate { get; set; } 

Uso formato:

grid.Column("WorkedDate", "Last Worked On", 
    format: (item) => item.WorkedDate != DateTime.MinValue ? 
    item.WorkedDate.ToString("MM/dd/yy") : "", canSort: true) 
0

Esto funcionó para mí:

grid.Column("Date_Of_Birth", "Date Of Birth", format: item => ((item.Date_Of_Birth == null) ? "" : item.Date_Of_Birth.ToString("MM/dd/yyyy"))) 
Cuestiones relacionadas