在 Razor 视图中与通用方法一起使用

发布于 2024-11-18 11:03:28 字数 2797 浏览 3 评论 0原文

有没有办法使 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想念有你 2024-11-25 11:03:28

如果将整个语句放在括号中会怎么样:

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

或者使用代码块?

HTH。

What if you wrap the whole statement in parens:

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

Or use a code block?

HTH.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文