Why can't I use my extension method as a delegate in Razor WebGrid

I am using MVC 3 introduced by WebGrid, but cannot help but apply my own extension methods when passing the delegate to the parameter format.

Using:

Grid.Column("MyProperty", "MyProperty", 
format: @<span class="something">@item.MyProperty.MyExtensionMethodForString()</span>)

I got

ERROR: 'string' does not contain a definition for 'MyExtensionMethodForString'

I tried casting, not using

Grid.Column("MyProperty", "MyProperty", 
format: @<span class="something">@((string)(item.MyProperty).MyExtensionMethodForString())</span>)

If I use the standard method, it works:

Grid.Column("MyProperty", "MyProperty", 
format: @<span class="something">@(Utils.MyExtensionMethodForString(item.MyProperty))</span>)

I also tried putting the extension in the same namespace without any results.

How can I use my favorite extensions?

Edit : a namespace as such is not a problem, a namespace where the extension is available for all views and classes, and I can use it in one view without problems. The problem is using it as a delegate.

+5
3

WebGrid Razor. # dynamic. . Func<dynamic, object>, item .

I .

+2

. :

public static class TestExtensions
{
    public static string Foo(this HtmlHelper html, Func<object, HelperResult> func)
    {
        return func(null).ToHtmlString();
    }

    public static string MyStringExtension(this string s)
    {
        return s.ToUpper();
    }
}

Index.cshtml:

@using MvcApplication1.Controllers

@Html.Foo(@<text>@Html.Raw("Hello")</text>)

:

Hello

Index.cshtml:

@using MvcApplication1.Controllers

@Html.Foo(@<text>@("Hello".MyStringExtension())</text>)

:

CS1061: "" "MyStringExtension", "MyStringExtension", "" ( using ?)

, , Razor. ( HtmlHelper , )

+1

SO , :

@a.GetAttribute<ActionLabelAttribute>().ActionLabel

, , , :

@(a.GetAttribute<ActionLabelAttribute>().ActionLabel)

. GetAttribute .

0

All Articles