【问题标题】:How can I use DisplayFormat data annotation in WebGrid columns?如何在 WebGrid 列中使用 DisplayFormat 数据注释?
【发布时间】:2011-09-12 00:32:23
【问题描述】:

我的模型中有以下属性:

[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime? Date { get; set; }

我正在尝试使用 Html.DisplayFor 助手在 WebGrid 列中使用此规范,如下所示:

Sources.Column("Date", "As Of Date", (item) => Html.DisplayFor(x => item))

当我运行它时,我在列中得到了很多额外的信息,并且日期以长格式日期出现,而不是所需的短格式。我得到的输出让我怀疑 DisplayFor 正在查看模型中的每个属性并打印它,而不是只查看日期。为什么会这样做?我可以做些什么来在 WebGrid 中使用 DisplayFor 吗?

当我尝试指定 item.Date 时,我收到错误“表达式树可能不包含动态操作”

【问题讨论】:

    标签: asp.net-mvc-3 razor html-helper webgrid


    【解决方案1】:

    试试这个代码 sn-p 而不是你现在的代码:

    Sources.Column( header: "Date", format: ( item -> { return @Html.Raw(item.date.ToString("d")); }))
    

    【讨论】:

      猜你喜欢
      • 2011-07-12
      • 1970-01-01
      • 2016-04-17
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多