Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebGrid Column Format Issue in MVC3

I've been trying to change the format of a single column in a WebGrid without much success. Said column is this:

grid.Column(
    columnName: "EmailAddress", 
    header: "Email Address", 
    format:(item) => Html.EmailLink(item.EmailAddress, item.EmailAddress, ""), 
    canSort: false
),

The error is:

The best overloaded method match for 'System.Web.Helpers.WebGrid.Column(string, string, System.Func, string, bool)' has some invalid arguments

I am confused as the method signature matches. Also, if I change the column to the below then it works without any errors:

grid.Column(
    columnName: "EmailAddress", 
    header: "Email Address", 
    format:(item) => new HtmlString(String.Format("<a href=\"mailto:{0}\" class=\"{2}\">{1}</a>", item.EmailAddress, item.EmailAddress, "")), 
    canSort: false
),

For reference, EmailLink is a very basic HtmlHelper extension method:

public static IHtmlString EmailLink(this HtmlHelper helper, string emailAddress, string linkText, string linkClass) {
    return new HtmlString(String.Format("<a href=\"mailto:{0}\" class=\"{2}\">{1}</a>", emailAddress, linkText, linkClass));
}

Can anyone tell me what the issue here is, and how I can solve it?

like image 892
Rory McCrossan Avatar asked Mar 22 '11 16:03

Rory McCrossan


1 Answers

This is due to the ugliness of WebGrid and all this dynamic crap. You need a cast:

grid.Column(
    columnName: "EmailAddress", 
    header: "Email Address", 
    format: item => Html.EmailLink(
        (string)item.EmailAddress, 
        (string)item.EmailAddress, 
        ""
    ), 
    canSort: false
)

This being said don't hesitate to checkout MvcContrib Grid or the Telerik Grid which are far better.

like image 76
Darin Dimitrov Avatar answered Oct 23 '22 14:10

Darin Dimitrov