有没有办法让razor视图中的using
语句与泛型方法一起使用?例如,我喜欢webforms代码段
<% using(Html.BeginForm<Controller>(c => c.Method()))
{ %>
Some code
<% } %>
像这样转换为剃刀
@using(Html.BeginForm<Controller>(c => c.Method()))
{
Some code
}
但这不起作用,因为razor将<Controller>
解释为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#
aynone知道怎么做吗?
更新:上面的第三种方法似乎就是这样做的。 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
}
答案 0 :(得分:7)
如果你将整个陈述包装在parens中怎么办?
@(using(Html.BeginForm<Controller>(c => c.Method())))
或者使用代码块?
HTH。