Translate to razor Syntax from MVC 2.0 code

I am trying to convert the popular asp.net MVC 2.0 solr.net sample app code to Razor syntax. I am not able to understand the last line ... Kindly help

 <% Html.Repeat(new[] { 5, 10, 20 }, ps => { %>
            <% if (ps == Model.Search.PageSize) { %>
            <span><%= ps%></span>
            <% } else { %>
            <a href="@Url.SetParameters(new {pagesize = ps, page = 1})">@ps</a>
            <% } %>
  <% }, () => { %> | <% }); %>

[update] Source for Html.Repeat extension

HtmlHelperRepeatExtensions.cs

Answers


For this to work you will have to modify the Html.Repeat extension method to take advantage of Templated Razor Delegates as illustrated by Phil Haack. And then:

@{Html.Repeat(
    new[] { 5, 10, 20 }, 
    @<text>
        @if (item == Model.Search.PageSize)
        {
            <span>@item</span>
        }
        else
        {
            <a href="@Url.SetParameters(new { pagesize = item, page = 1 })">
                @item
            </a>
        }
    </text>,
    @<text>|</text>
);}

UPDATE:

According to your updated question you seem to be using a custom HTML helper but as I stated in my answer you need to updated this helper to use the Templated Razor Delegates syntax if you want it to work. Here's for example how it might look:

public static class HtmlHelperRepeatExtensions
{
    public static void Repeat<T>(
        this HtmlHelper html,
        IEnumerable<T> items,
        Func<T, HelperResult> render,
        Func<dynamic, HelperResult> separator
    )
    {
        bool first = true;
        foreach (var item in items)
        {
            if (first)
            {
                first = false;
            }
            else
            {
                separator(item).WriteTo(html.ViewContext.Writer);
            }

            render(item).WriteTo(html.ViewContext.Writer);
        }
    }
}

or if you want to have the helper method directly return a HelperResult so that you don't need to use the brackets when calling it:

public static class HtmlHelperRepeatExtensions
{
    public static HelperResult Repeat<T>(
        this HtmlHelper html,
        IEnumerable<T> items,
        Func<T, HelperResult> render,
        Func<dynamic, HelperResult> separator
    )
    {
        return new HelperResult(writer =>
        {
            bool first = true;
            foreach (var item in items)
            {
                if (first)
                    first = false;
                else
                    separator(item).WriteTo(writer);
                render(item).WriteTo(writer);
            }
        });
    }
}

and then inside your view:

@Html.Repeat(
    new[] { 5, 10, 20 }, 
    @<text>
        @if (item == Model.Search.PageSize)
        {
            <span>@item</span>
        }
        else
        {
            <a href="@Url.SetParameters(new { pagesize = item, page = 1 })">
                @item
            </a>
        }
    </text>,
    @<text>|</text>
)

Possibly something like:

}, () => { <text>|</text> });


Need Your Help

How to prevent Unicode characters from rendering as emoji in HTML from JavaScript?

javascript html css unicode emoji

I'm finding Unicode for special characters from FileFormat.Info's search.

Can the performance of DbLinq's object tracking be improved?

c# mysql dblinq

I have an ASP.NET MVC2 site connecting to a MySQL database via DbLinq. There is a particular set of actions that get done at regular intervals on the site, which includes looping over a particular...