ASP.NET MVC3 Razor - How to conditionally quit or

2019-06-15 06:45发布

问题:

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

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

回答1:

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);}
}


回答2:

Invert the if:

<p>html that I always want</p>
@if (Model != null)
{
      your html when model != null
}