微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc-3 – 在剃刀视图中使用泛型方法

有没有办法让razor视图中的using语句与泛型方法一起使用?例如,我想要webforms代码

<% using(Html.BeginForm<Controller>(c => c.Method()))
   { %>
       Some code
<% } %>

像这样转换成剃刀

@using(Html.BeginForm<Controller>(c => c.Method()))
{
    Some code
}

但这不起作用,因为剃刀解释< 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
}

解决方法

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

@(using(Html.BeginForm<Controller>(c => c.Method())))

或者使用代码块?

HTH.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐