【问题标题】:Show date format only and keep sorting AZ in MVCContrib Grid仅显示日期格式并在 MVCContrib Grid 中继续对 AZ 进行排序
【发布时间】:2012-03-29 11:08:41
【问题描述】:

我使用以下 LINQ 创建网格模型:

...
            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 根本不起作用。

有什么线索吗? 谢谢!!!

【问题讨论】:

标签: asp.net-mvc-3 datetime mvccontrib


【解决方案1】:

尝试使用

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

【讨论】:

    【解决方案2】:

    我发现比较容易的是列上的 Format 方法。 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");
    

    【讨论】:

      猜你喜欢
      • 2021-05-08
      • 2018-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      • 2011-11-19
      • 1970-01-01
      • 2018-12-31
      相关资源
      最近更新 更多