3

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 DisplayTemplate in webgrid column ?

regrads.

Amar Palsapure
  • 9,590
  • 1
  • 27
  • 46
waldecir
  • 376
  • 2
  • 8
  • 21

2 Answers2

0

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • this dosent work, for some reason thsi display all values of model concatenated in the column – waldecir Apr 19 '11 at 22:39
  • 1
    if i try to do `grid.Column(columnName: "Expires", header: @Resources.Localization.Expires, format: @@Html.DisplayFor(x => item.Expires))`, i got this error _CS1963: An expression tree may not contain a dynamic operation_ – waldecir Apr 20 '11 at 01:13
  • @waldecir, try `@Html.DisplayFor(x => item)` as in my answer. – Darin Dimitrov Apr 20 '11 at 05:08
  • I have also tried this and it presents some strange results. Can you shed any more light on this? – Banford May 16 '11 at 09:28
0

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);
McDowell
  • 107,573
  • 31
  • 204
  • 267
StanB
  • 29
  • 3