Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC3 Razor - How to conditionally quit or end or return or break a partial view?

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

@if (Model == null)
{
    return;
}
like image 728
user202448 Avatar asked Feb 15 '11 21:02

user202448


2 Answers

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);}
}
like image 97
Darin Dimitrov Avatar answered Sep 18 '22 22:09

Darin Dimitrov


Invert the if:

<p>html that I always want</p>
@if (Model != null)
{
      your html when model != null
}
like image 44
Felipe Pessoto Avatar answered Sep 17 '22 22:09

Felipe Pessoto