Question

I am using webgrid to displat list of records.

My view is tightly coupled with IEnumerable.

@model IEnumerable<Models.SitesConfig.Configuration>

I am binding webgrid with Model.

var grid = new WebGrid(Model, rowsPerPage: 50);

I am trying to format a column using @helper method. @helper method taking a parametr of type Models.SitesConfig.Configuration.

When I try to load the view, I am getting invalid arguments error.

This is my view.

    @model IEnumerable<Models.SitesConfig.SiteConfiguration>
@section Styles {
    <link href="@Url.Content("~/Content/SatelliteSite.css")" rel="stylesheet" type="text/css" />
}

@{
    ViewBag.Title = "List of Satellite Sites";
}
@helper FormatTemplateColors(Models.SitesConfig.SiteConfiguration item)
{
    @String.Format(
        "Border: {0} <br/>Link: {1} <br/>Text: {2}",
        item.BorderColor != null ? item.BorderColor.Name : string.Empty,
        item.LinkColor != null ? item.LinkColor.Name : string.Empty,
        item.TextColor != null ? item.TextColor.Name : string.Empty)
}
@{
    var grid = new WebGrid(Model, rowsPerPage: 50);
}
<div>
    @grid.GetHtml(columns: grid.Columns(
        grid.Column("Template", 
            style: "color-column-width", 
            format:(item) => @FormatTemplateColors(item)
        )
         )
</div>

Can somebody help me on this.

Was it helpful?

Solution

In the format lambda the item parameter is an instance of the WebGridRow class (in a form of dynamic) where the Value property holds the actual item.

So you need to write:

format:(item) => @FormatTemplateColors(item.Value)

SideNote if you wan't to output html you need to use the Html.Raw helper. So modify your helper to:

@helper FormatTemplateColors(Models.SitesConfig.SiteConfiguration item)
{
    @Html.Raw(String.Format(...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top