【问题标题】:Set a DisplayTemplate to a WebGrid Column将 DisplayTemplate 设置为 WebGrid 列
【发布时间】:2011-04-19 21:53:17
【问题描述】:

我有这个样本

@Html.DisplayFor(modelItem => item.Expires, "DateTimeToDate" )

DateTimeToDate 显示模板:

@model DateTime?
@Html.Label(Model.HasValue ? Model.Value.ToShortDateString(): string.Empty)

栏目

... grid.Column("Expires", @Resources.Localization.Expires, canSort: true) ...

那么,我如何在 webgrid 列中使用 DisplayTemplate ?

毕业了。

【问题讨论】:

    标签: c# asp.net asp.net-mvc-3


    【解决方案1】:

    如果您的视图模型的 Expires 属性是 DateTime 并且您有一个自定义显示模板,您可以尝试以下操作:

    grid.Column("Expires", format: @<text>@Html.DisplayFor(x => item)</text>)
    

    这应该为视图模型中每个项目的 Expires 属性呈现 ~/Views/Shared/DisplayTemplates/DateTime.cshtml

    【讨论】:

    • 这项工作,由于某种原因,它会显示列中连接的所有模型值
    • 如果我尝试做grid.Column(columnName: "Expires", header: @Resources.Localization.Expires, format: @&lt;text&gt;@Html.DisplayFor(x =&gt; item.Expires)&lt;/text&gt;),我得到了这个错误CS1963:表达式树可能不包含动态操作
    • @waldecir,在我的回答中尝试@Html.DisplayFor(x =&gt; item)
    • 我也试过这个,它呈现出一些奇怪的结果。您能否对此进行更多说明?
    【解决方案2】:

    这是一个对我有用的 sn-p,您需要根据自己的情况进行调整..

    WebGridColumn adminColumn = grid.Column(columnName: string.Empty,
      header: string.Empty,
      format: (item) => new HtmlString(Html.ActionLink("Edit", "Edit",
        new { id = item.Id }, null).ToString() + "|" +
        Html.ActionLink("Delete", "Delete", new { id = item.Id }, null).ToString()));
    columns.Add(adminColumn);
    

    【讨论】:

      【解决方案3】:

      我发现this article 完全符合您的需求,我猜...

      可惜你问的时候还没写完!

      【讨论】:

        猜你喜欢
        • 2013-02-13
        • 2013-07-22
        • 2013-06-25
        • 1970-01-01
        • 2013-12-02
        • 2012-02-17
        • 2012-02-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多