Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use ForEach extension in Razor

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?

like image 563
Miguel Moura Avatar asked May 06 '14 15:05

Miguel Moura


2 Answers

Your problem is that Razor expects your ForEach method to return a value, such as MvcHtmlString.

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:

public static MvcHtmlString ForEach<T>(this IEnumerable<T> source, Func<T, Int32, string> selector)
{
    return new MvcHtmlString(String.Join("\n", source.Select(selector)));
}

Usage:

@Model.ForEach((card, i) =>
{
    return "<li class=c" + i + ">" + card.Text + "</li>";
})
like image 195
Nathan A Avatar answered Nov 09 '22 10:11

Nathan A


This works, but it feels hackish:

public static HelperResult ForEach<T>(this IEnumerable<T> source, Func<T, int, Func<TextWriter, HelperResult>> action) {
  return new HelperResult((writer) => {
    int i = 0;
    foreach (T item in source) {
      action(item, i)(writer).WriteTo(writer);
      i++;
    }
  });
}

@Model.ForEach((card, i) => 
    @<li class="c@(i)">@card.Text</li>
)
like image 33
Ondrej Svejdar Avatar answered Nov 09 '22 08:11

Ondrej Svejdar