简体   繁体   中英

Html.DisplayFor in mvc webgrid

Using an asp.net mvc webgrid, is it possible to render a column 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM