How can I use DisplayFormat data annotation in WebGrid columns?
I have the following property in my model:
[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime? Date { get; set; }
A开发者_运维技巧nd I am trying to use the Html.DisplayFor
helper to use this specification in a WebGrid column, like so:
Sources.Column("Date", "As Of Date", (item) => Html.DisplayFor(x => item))
When I run this, I get a lot of extra information in the column, and the date comes out as a long format date, instead of the desired short format. The output I get makes me suspect that DisplayFor is looking through each property in the model and printing it, instead of just looking at Date. Why would it do this? Is there something I can do to use DisplayFor in the WebGrid?
When I try to specify item.Date
I get the error "An expression tree may not contain a dynamic operation"
Try this code snippet instead of what you have now:
Sources.Column( header: "Date", format: ( item -> { return @Html.Raw(item.date.ToString("d")); }))
精彩评论