加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 07:09:51 所属栏目:asp.Net 来源:网络整理
导读:有没有办法让razor视图中的using语句与泛型方法一起使用?例如,我想要webforms代码段 % using(Html.BeginFormController(c = c.Method())) { % Some code% } % 像这样转换成剃刀 @using(Html.BeginFormController(c = c.Method())){ Some code} 但这不起作用
有没有办法让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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读