在 Razor 视图中与通用方法一起使用
有没有办法使 using
语句与 razor 视图中的通用方法一起使用?例如,我希望将 Webforms 代码片段
<% using(Html.BeginForm<Controller>(c => c.Method()))
{ %>
Some code
<% } %>
转换为像这样的 razor,
@using(Html.BeginForm<Controller>(c => c.Method()))
{
Some code
}
但这不起作用,因为 razor 将
解释为 HTML 标记。添加括号也不起作用,因为此时 razor 不包含开始和结束 BeginForm
的大括号。以下是我尝试过的不同方法,我想不出更多了。
@using(Html.BeginForm<Controller>(c => c.Method())) // Interpreted as c# to '<Controller>'
{ // interpreted as HTML
Some code // interpreted as HTML
} // interpreted as HTML
@(using(Html.BeginForm<Controller>(c => c.Method()))) // Interpreted as c#
{ // interpreted as HTML
Some code // interpreted as HTML
} // interpreted as HTML
@{using(Html.BeginForm<Controller>(c => c.Method())) // Interpreted as c#
{ // interpreted as c#
Some code // interpreted as c#
} // interpreted as c#
} // interpreted as c#
@(using(Html.BeginForm<Controller>(c => c.Method()))) // Interpreted as c#
@{ // interpreted as c#
Some code // interpreted as c#
} // interpreted as c#
有人知道怎么做吗?
更新:似乎上面的第三种方法就是这样做的方法。 Razor 显然是这样工作的:
@{using(Html.BeginForm<Controller>(c => c.Method())) // Interpreted as c#
{ // interpreted as c#
<p> // interpreted as HTML
Some text // interpreted as HTML
@Code // interpreted as c#
</p> // interpreted as HTML
} // interpreted as c#
} // interpreted as c#
这不是最明显的做事方式,但它确实有效。
更新 2:Razor 可能在某个时候进行了更新,因为现在上面的选项 #1 可以按预期工作。
@using(Html.BeginForm<Controller>(c => c.Method()))
{
Some code
}
Is there a way to make the using
statement work with generic methods in razor views? For example I'd like the webforms snippet
<% using(Html.BeginForm<Controller>(c => c.Method()))
{ %>
Some code
<% } %>
converted to razor like this
@using(Html.BeginForm<Controller>(c => c.Method()))
{
Some code
}
but that does not work, since razor interprets <Controller>
as an HTML tag. Adding parentheses does not work either, since then razor does not include the curly brackets that begin and end the BeginForm
. Below are the different approaches I've tried, and I can't think of any more.
@using(Html.BeginForm<Controller>(c => c.Method())) // Interpreted as c# to '<Controller>'
{ // interpreted as HTML
Some code // interpreted as HTML
} // interpreted as HTML
@(using(Html.BeginForm<Controller>(c => c.Method()))) // Interpreted as c#
{ // interpreted as HTML
Some code // interpreted as HTML
} // interpreted as HTML
@{using(Html.BeginForm<Controller>(c => c.Method())) // Interpreted as c#
{ // interpreted as c#
Some code // interpreted as c#
} // interpreted as c#
} // interpreted as c#
@(using(Html.BeginForm<Controller>(c => c.Method()))) // Interpreted as c#
@{ // interpreted as c#
Some code // interpreted as c#
} // interpreted as c#
Does aynone know how to do this?
Update: It seems the third way above is the way to do this. Razor apparently works like this:
@{using(Html.BeginForm<Controller>(c => c.Method())) // Interpreted as c#
{ // interpreted as c#
<p> // interpreted as HTML
Some text // interpreted as HTML
@Code // interpreted as c#
</p> // interpreted as HTML
} // interpreted as c#
} // interpreted as c#
Not the most obvious way of doing things, but it works.
Update 2: Razor has probably been updated at some point, because now option #1 above works as expected.
@using(Html.BeginForm<Controller>(c => c.Method()))
{
Some code
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果将整个语句放在括号中会怎么样:
或者使用代码块?
HTH。
What if you wrap the whole statement in parens:
Or use a code block?
HTH.