问题描述
我正在尝试将流行的 asp.net MVC 2.0 solr.net 示例应用程序代码转换为 Razor 语法.我无法理解最后一行...请帮助
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>
<% } %>
<% }, () => { %> | <% }); %>
[更新]Html.Repeat 扩展的来源
HtmlHelperRepeatExtensions.cs
推荐答案
为此,您必须修改 Html.Repeat
扩展方法以利用 模板化 Razor 代表,如 Phil Haack 所示.然后:
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>
);}
<小时>
更新:
根据您更新的问题,您似乎正在使用自定义 HTML 帮助程序,但正如我在回答中所述,您需要更新此帮助程序以使用 Templated Razor Delegates 语法,如果您希望它工作.例如,它的外观如下:
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);
}
}
}
或者如果你想让辅助方法直接返回一个 HelperResult 这样你在调用它时就不需要使用括号:
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);
}
});
}
}
然后在你的视野中:
@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>
)
这篇关于从 MVC 2.0 代码转换为 razor 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!