ASP.NET MVC3 Razor - How to conditionally quit or

2019-06-15 06:42发布

With Razor, how do you conditionally quit or end or return or break a partial view?

@if (Model == null)
{
    return;
}

2条回答
小情绪 Triste *
2楼-- · 2019-06-15 07:19

No, you don't return in a view, you simply don't include such partial in the main view:

@if (Model != null) {
    @Html.Partial("somePartial", Model)
}

or if you use RenderPartial:

@if (Model != null) {
    @{Html.RenderPartial("somePartial", Model);}
}
查看更多
贼婆χ
3楼-- · 2019-06-15 07:33

Invert the if:

<p>html that I always want</p>
@if (Model != null)
{
      your html when model != null
}
查看更多
登录 后发表回答