在剃刀视图中使用泛型方法

nfo*_*rss 5 generics using razor asp.net-mvc-3

有没有办法使using语句在剃刀视图中使用泛型方法?例如,我想要webforms代码段

<% using(Html.BeginForm<Controller>(c => c.Method()))
   { %>
       Some code
<% } %>
Run Code Online (Sandbox Code Playgroud)

像这样转换成剃刀

@using(Html.BeginForm<Controller>(c => c.Method()))
{
    Some code
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为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#    
Run Code Online (Sandbox Code Playgroud)

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#
Run Code Online (Sandbox Code Playgroud)

不是最明显的做事方式,但它有效.

更新2:Razor可能已在某个时候更新,因为现在上面的选项#1按预期工作.

@using(Html.BeginForm<Controller>(c => c.Method()))
{
    Some code
} 
Run Code Online (Sandbox Code Playgroud)

Bri*_*ins 7

如果你将整个陈述包装在parens中怎么办:

@(using(Html.BeginForm<Controller>(c => c.Method())))
Run Code Online (Sandbox Code Playgroud)

或者使用代码块?

HTH.