Show only date format and keep AZ sorting in MVCContrib Grid
I am using the following LINQ to create a mesh model:
...
var query = from a in GetUser()
select new UserModel
{
ID = a.ID,
StartDate = a.StartDate,
EndDate = a.EndDate,
StateName = a.State.StateName,
StateID = a.StateID,
City = a.City,
};
return query;
....
and HTML
@Html.Grid(Model.PagedList).Columns(
col =>
{
col.For(c => c.StateName).Named("State").Attributes(@class => "row").HeaderAttributes(@class => "head");
col.For(c => c.City).Named("City").Attributes(@class => "row").HeaderAttributes(@class => "head");
col.For(c => c.StartDate).Named("Start Date").Attributes(@class => "row").HeaderAttributes(@class => "head");
col.For(c => c.EndDate).Named("End Date").Attributes(@class => "row").HeaderAttributes(@class => "head");
col.For(c => Html.ActionLink("Details", "Details", new { id = c.ID })).Named("View Details").HeaderAttributes(@class => "head").DoNotEncode();
}).Sort(Model.GridSortOptions).Attributes(@class => "grid")
The main problem is how to show only DATE without the TIME part?
I tried some options, but in some cases I got errors, and in other cases the AZ sort doesn't work at all.
Any hint? Thank!
+3
source to share
2 answers
Something a little easier, I find the Format on Column method . see docs here
So your code looks something like this:
col.For(c => c.EndDate).Format("{0:d"})
//use .Format("{0:yyyy-MM-dd}") to get 2014-10-21
If you want to use ToShortDateString you will need to define the column name as well as the sort column name, for example:
col.For(c => c.EndDate.ToShortDateString())
.Named("End Date")
.SortColumnName("EndDate");
+3
source to share