I am using an IEnumerable extension to loop through a collection and also get its index:
@Model.ForEach((card, i) => {
@<li class="c@(i)">@card.Text</li>;
})
The ForEach extension is the following:
public static void ForEach<T>(this IEnumerable<T> source, Action<T, Int32> action) {
Int32 i = 0;
foreach (T item in source) {
action(item, i);
i++;
}
} // ForEach
But when I try to compile it I get the following error message:
Argument 1: cannot convert from 'void' to 'System.Web.WebPages.HelperResult'
How can I solve this? Do I need a new extension?
Your problem is that Razor expects your
ForEach
method to return a value, such asMvcHtmlString
.Also, you cannot simply expect Razor to translate the body of your foreach clause to work with your Action delegate. There is a way to get this to work, but it's not as pretty. I'd suggest using the
@for(...)
clause as James suggested if you want to use the standard razor syntax.However, here's an implementation that does work:
Usage:
This works, but it feels hackish: