用于在Html.BeginForm中包装一些html的条件逻辑

ken*_*ner 1 asp.net-mvc razor

我有一个看起来像这样的剃刀视图

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

    <input type="submit" value="Submit!" />
}
Run Code Online (Sandbox Code Playgroud)

现在我只想渲染表单并提交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!" />
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当然,这种语法不起作用.有人知道这样做的好方法吗?

ken*_*ner 5

最后使用剃刀助手.我认为这是最好的方法,如果你有一个好的方法,仍然不介意看到替代方案

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