开发者

Set a DisplayTemplate to a WebGrid Column

开发者 https://www.devze.com 2023-02-27 08:13 出处:网络
i have this sample @Html.DisplayFor(modelItem => item.Expires, \"DateTimeToDate\" ) DateTimeToDate Display Template:

i have this sample

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

DateTimeToDate Display Template:

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

Column

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

So, how i can use Disp开发者_开发问答layTemplate in webgrid column ?

regrads.


If the Expires property of your view model is DateTime and you have a custom display template for it you may try the following:

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

This should render ~/Views/Shared/DisplayTemplates/DateTime.cshtml for the Expires property of each item in the view model.


Here is a snippet that worked for me, you will need to adjust it to your situation..

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


I found this article matching exactly your need I guess...

Too bad it was not written yet when you asked the question!

0

精彩评论

暂无评论...
验证码 换一张
取 消