3

I use the following LINQ to create grid 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 the HTML is

 @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 TIME part?

I tried some options but in some cases I got the errors and in other cases sorting AZ doesn't work at all.

Any clue? Thank you!!!

NoWar
  • 36,338
  • 80
  • 323
  • 498
  • 1
    try to look at this post http://stackoverflow.com/questions/4679352/converting-datetime-format-using-razor – COLD TOLD Mar 13 '12 at 02:11

2 Answers2

4

Try using

col
.For(c => c.EndDate.ToLongDateString())
.Named("End Date")
.Attributes(@class => "row")
.HeaderAttributes(@class => "head");
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
gurmandeep
  • 1,227
  • 1
  • 14
  • 30
3

Something a little easier I find is the Format method on the column. 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 then 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");
Dai Bok
  • 3,451
  • 2
  • 53
  • 70