【问题标题】:ASP.NET MVC3 Razor - How to conditionally quit or end or return or break a partial view?ASP.NET MVC3 Razor - 如何有条件地退出或结束或返回或中断部分视图?
【发布时间】:2011-02-15 21:57:15
【问题描述】:

使用 Razor,您如何有条件地退出或结束或返回或中断部分视图?

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

【问题讨论】:

标签: asp.net asp.net-mvc-3 razor


【解决方案1】:

不,您不会在视图中return,您根本不会在主视图中包含这样的部分:

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

或者如果你使用RenderPartial:

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

【讨论】:

    【解决方案2】:

    反转 if:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多