Html.DisplayFor in mvc webgrid
Using an asp.net mvc webgrid, is it possible to render a column开发者_JAVA技巧 using Html.DisplayFor for the current row/column?
grid.Column("Roller", "Roller", canSort: true, format: @<text>@Html.DisplayFor( <the row result here> )</text>)
The Html.DisplayFor(m) helper uses the page model, not the current row item. Is there a way around this.
Thanks
// Johan
Yes, it is possible. For an example, consider you are binding a list of Banner
objects to your WebGrid. Also consider the Banner.Active
property, which is a boolean
value that you want to be rendered as a CheckBox. You can do this:
format: (item) => { var banner = item.Value as Banner;
return Html.DisplayFor(modelItem => banner.Active);
}
You could also do this:
format: (item) => Html.DisplayFor(modelItem => ((item as WebGridRow).Value as Banner).Active)
But I would consider the first option more readable.
try this
var grid = new WebGrid(Model);
than do
grid.Column("Roller", "Roller", canSort: true, format: @<text>@Html.DisplayFor(modelItem => item.blabla)</text>)
Using item.blabla"whatever ur item name is" may work but im not really experienced about this.
also there is not much difference between
@<text>@Html.DisplayFor(modelItem => item.blabla)</text>
&
@<text>@item.blabla</text>
both usage does the job.
精彩评论