man-dart
man-dart

Reputation: 13

Call to Func<string,string> on razor is being ToString'ed instead of executed

Imagine I have this simple method:

public string A ( string key )
{
    return "_" + key + "_";
}

In the constructor of a class I say this:

public Func<string, string> F;

public AClass( )
{
    F = A;
}

In a controller I return the Func like this:

var a = new AClass();
ViewBag.MyFunc= a.F;

However, something like this in a view

@{
    var X = ViewBag.MyFunc;
}

// ...

<h1 class="whatever" style="float:left">@X ( "hello" )</h1>

means I get a ToString() on the F func rendered, instead of the actual method call I'd like to see, so instead of the output of the silly function A above, I see this

System.Func`2[System.String,System.String] ( "hello" ) 

Criticisms on why I should not be doing this are welcome, but what am I missing here? I think I am blind on something obvious.

thanx

Upvotes: 1

Views: 73

Answers (2)

Mark Segal
Mark Segal

Reputation: 5550

It seems that the problem is the spacing.

Try

<h1 class="whatever" style="float:left">@X("hello")</h1>

And if this still doesn't work, try this:

<h1 class="whatever" style="float:left">@(X("hello"))</h1>

Upvotes: 0

SLaks
SLaks

Reputation: 887767

You need to remove the space before the (, so that the Razor parser realizes that it's part of your @ code.

Upvotes: 1

Related Questions