Asp.net mvc 3 仅显示日期格式,并在MVCContrib网格中保持AZ排序
我使用以下LINQ创建网格模型:Asp.net mvc 3 仅显示日期格式,并在MVCContrib网格中保持AZ排序,asp.net-mvc-3,datetime,mvccontrib,Asp.net Mvc 3,Datetime,Mvccontrib,我使用以下LINQ创建网格模型: ... var query = from a in GetUser() select new UserModel { ID = a.ID, StartDate = a.Start
...
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")
主要问题是如何只显示日期而不显示时间部分?
我尝试了一些选择,但在某些情况下,我得到了错误,在其他情况下,排序AZ根本不起作用
有线索吗?
谢谢你 尝试使用
col
.For(c => c.EndDate.ToLongDateString())
.Named("End Date")
.Attributes(@class => "row")
.HeaderAttributes(@class => "head");
我发现更简单的是列上的格式方法 因此,您的代码如下所示:
col.For(c => c.EndDate).Format("{0:d"})
//use .Format("{0:yyyy-MM-dd}") to get 2014-10-21
如果要使用ToSortDateString,则需要定义列名和排序列名,例如:
col.For(c => c.EndDate.ToShortDateString())
.Named("End Date")
.SortColumnName("EndDate");
试着看看这篇文章