Reputation: 13
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
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
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