Just in case anyone else finds this post (as I did), Andy's Update is almost there. In addition to the example given, all you have to do to access the 'int' in the example given is reference @item
. In @
blocks, the variable item
contains the model it was called on.
Here's an example of how it can be used:
@model PageData
@{
Func sayHi =
@
- Hello @(item.FirstName)!
;
}
@foreach(var customer in Model.Customers)
{
sayHi(customer);
}
In most instances you should probably use a partial view instead of a function like this. But in the rare instances that a partial view is not possible (such as when using the RazorEngine library), this works.