How to format date in mvc web grid

Crime Master Gogo picture Crime Master Gogo · Sep 7, 2011 · Viewed 38.3k times · Source

I have a web grid and I am not using razor syntex.Rather I am using the .aspx form. the code is below;

<%
    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 }))
                  )
               )
    %>

Now I want to format the 'PublishDate' column to something like 'dd-MMM-yyyy'. Any idea how to do this?

Answer

Darin Dimitrov picture Darin Dimitrov · Sep 7, 2011
grid.Column(
    "PublishDate", 
    "Published on",
    format: (item) => string.Format("{0:dd-MMM-yyyy}", item.PublishDate)
)