Translate to razor Syntax from MVC 2.0 code(从 MVC 2.0 代码转换为 razor 语法)
问题描述
我正在尝试将流行的 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 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 MVC 2.0 代码转换为 razor 语法
- 如何用自己压缩一个 IEnumerable 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- C# 中多线程网络服务器的模式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01