NoWar
NoWar

Reputation: 37633

Show date format only and keep sorting AZ in MVCContrib Grid

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!!!

Upvotes: 3

Views: 911

Answers (2)

Dai Bok
Dai Bok

Reputation: 3606

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");

Upvotes: 3

gurmandeep
gurmandeep

Reputation: 1247

Try using

col
.For(c => c.EndDate.ToLongDateString())
.Named("End Date")
.Attributes(@class => "row")
.HeaderAttributes(@class => "head");

Upvotes: 4

Related Questions