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})" rel="nofollow noreferrer noopener">@ps</a>
            <% } %>
  <% }, () => { %> | <% }); %>

[update]
Source for Html.Repeat extension
HtmlHelperRepeatExtensions.cs

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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 })" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">
                @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 })" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">
                @item
            </a>
        }
    </text>,
    @<text>|</text>
)

Method 2

Possibly something like:

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x