2012-03-13 30 views
3

我用下面的LINQ创建网格模型:显示的日期格式只,并保持在MVCContrib排序AZ电网

... 
      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; 
    .... 

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

的主要问题是如何显示只有DATE没有TIME部分?

我尝试了一些选项,但在某些情况下,我得到了错误,而在其他情况下,排序AZ根本不起作用。

任何线索? 谢谢!!!

+1

试着看看这篇文章http://stackoverflow.com/questions/4679352/converting-datetime-format-using-razor – 2012-03-13 02:11:33

回答

4

尝试使用

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

的东西更容易一些,我觉得是塔上的格式方法。 see docs here

所以,你的代码看起来是这样的:

col.For(c => c.EndDate).Format("{0:d"}) 
//use .Format("{0:yyyy-MM-dd}") to get 2014-10-21 

如果你想使用ToShortDateString,那么你将需要定义列名以及排序列名,例如:

col.For(c => c.EndDate.ToShortDateString()) 
    .Named("End Date") 
    .SortColumnName("EndDate");