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?
In Razor, `@` symbol is used to transition from HTML to C#. To escape an '@' symbol in razor markup, use two '@' symbols.
ViewData is a container for data to be passed from the PageModel to the content page. ViewData is a dictionary of objects with a string-based key. You add items to ViewData as follows: public class IndexModel : PageModel. {
Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML.
Razor is a markup syntax for embedding . NET based code into webpages. The Razor syntax consists of Razor markup, C#, and HTML. Files containing Razor generally have a . cshtml file extension.
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With