gpt4 book ai didi

asp.net-mvc - 在 Html.BeginForm 中包装一些 html 的条件逻辑

转载 作者:行者123 更新时间:2023-12-02 15:40:39 24 4
gpt4 key购买 nike

我有一个像这样的 Razor View

@using(Html.BeginForm(MVC.Account.Info()))
{
<p>blah blah blah</p>

<input type="submit" value="Submit!" />
}

现在我只想呈现表单并提交 if User.Identity.IsAuthenticated

如果缩进级别很重要,可能看起来像这样

@if(User.Identity.IsAuthenticated) // wraps the using and its open brace
{
@using(Html.BeginForm(MVC.Account.Info()))
{
}

<p>blah blah blah</p>

@if(User.Identity.IsAuthenticated) // wraps the input and the using closing brace
{
<input type="submit" value="Submit!" />
}
}

当然,这种语法是行不通的。有人知道这样做的好方法吗?

最佳答案

最终使用了 Razor 助手。我认为这是最好的方法,如果你有好的方法,仍然不介意看到其他选择

@helper Info()
{
<p>blah blah blah</p>
}

@if(User.Identity.IsAuthenticated)
{
using(Html.BeginForm(MVC.Account.Info()))
{
@Info()
<input type="submit" value="Submit!" />
}
}
else
{
@Info()
}

关于asp.net-mvc - 在 Html.BeginForm 中包装一些 html 的条件逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7035848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com