Many devs do this:
public void foo() {
if (flag) {
// do stuff
}
}
I prefer to "return early", and so do this instead:
public void foo() {
if (!flag) return;
// do stuff
}
In an ASP.NET-MVC Razor view, what is the correct way to abort/skip/cancel rendering of a view/partialview? For example how do I convert this:
@if (flag) {
// do stuff
}
to something like this:
@if (!flag) { /* what do I do here to abort/skip/cancel the rendering? */ }
// do stuff
I've tried using return
and playing with the Request
, but am unsure how this affects the process. What is the correct way to do this?
As I mentioned above, you can simply issue a return
.
I recall cshtml files are compiled at runtime, which includes all inline code as well as the static html. That means that in theory, I'd expect any code to be left as-is and not transformed in any way.
So if you do this:
@if (!flag) { return; }
// do stuff
It works as expected (well for me at least). I just wanted to know if this leads to any unintended side-effects.
If with "abort" you mean you want to exclude something according to that flag then you have nothing to do, just use if
to delimit such sections (eventually it may even be from that line to the end of file). Like this:
@if (Model.User.HasEditingPrivileges)
{
<input type="button" id="edit" value="Edit"/>
}
Quickly it'll become hard to understand so you may use partial views for that (especially if blocks you have to include/exclude are big):
@if (Model.User.HasEditingPrivileges)
{
Html.RenderPartial("EditSection");
}
If you need to drop/skip/cancel page creation (for example to redirect to another page or to display something completely different), like this:
@if (!Model.User.hasEditingPrivileges)
{
// Ooops, he shouldn't see this page, go back to Home!
}
Then you're doing that check in the wrong place. Do it in your controller, it'll pick right view and views won't be aware of such logic:
public ActionResult View(int id)
{
if (HasUserEditingPrivileges)
return View("Edit", new MyModel(id));
return Redirect("UnauthorizedAccess"); // Oops, something went wrong
}
Why not?
- Because views shouldn't be aware of such logic. If you need something like that (CGI like sequential flow) then you shouldn't use MVC because it adds a complexity you don't need.
- Because you can't (unless someone find a terrible dirty hacky trick). MVC is structured to build a page when it has to be displayed (controller decides which page and with which data). When this building starts an output is required (unless you throw an exception to signal an error but you should really avoid exceptions to handle program flow then...). For small ifs you may simply use first mentioned method.
To summarize:
In an ASP.NET-MVC Razor view, what is the correct way to abort rendering of a view/partialview?
There is not a correcty way because you must not do it. Code in view has to render the page, it has not to decide which page should be sent to client. If you're doing anything else then you're using MVC as it shouldn't be used, it's just not correct.