stephen776
stephen776

Reputation: 9234

Are IF..ELSE statements in a View frowned upon in ASP.NET MVC?

I know that you want to keep logic out of your views. I can elimate most loops by using DisplayFor/EditorFor and passing IEnumerables to the view.

What about IF statements? should they be avoided completely in views? used sparingly? as a last resort?

Say you wanted to show hide an element based on a User role...How would u go about doing this without an IF statement...a completely seperate view perhaps?

Just trying to get an idea of best practices.

Thanks!

Upvotes: 7

Views: 2644

Answers (7)

ataddeini
ataddeini

Reputation: 4951

Rob Conery has a rule of thumb that states "if there's and IF, make a helper". Personally, I would say "use sparingly". I avoid it as much as possible because it makes things more difficult to unit test.

For the situation where you want to hide elements based on user roles: For simple scenarios, I would probably put the check directly in the view. Usually I try to still make these more terse and testable, though. So instead of:

@if (HttpContext.Current.User.IsInRole("admin")
{
    // Show admin stuff
}

I would do something like:

@if (Model.UserIsAdmin)
{
    // Show admin stuff
}

On the other hand, if these kinds of checks started getting speckled all over your views, I'd probably create the elements conditionally in the viewmodel first, and then just display what's been built. Hope that helps.

Upvotes: 3

VikciaR
VikciaR

Reputation: 3412

I would suggest to hide element by "if" in the view, but in code you must disable function (method), which is activated by hidden element.

Upvotes: 0

manojlds
manojlds

Reputation: 301167

If / else can be used sparingly, in my opinion, but for the example you mention, hide an element based on role - the if check should definitely not be in the view. Write extensions and helpers where needed.

Upvotes: 0

Andrew Barber
Andrew Barber

Reputation: 40150

Be consistent, and keep in mind the purpose of the view - to produce your HTML. Toward that end, certainly you will need some if constructs here or there. I think some people are suggesting you stick to some pie-in-the-sky, ultra-nitpicky purism here at the expense of usable, functional, well-defined code.

Upvotes: 14

SLaks
SLaks

Reputation: 887459

There is nothing wrong with using ifs in your views, as long as you don't end up putting backend logic in them.

Upvotes: 10

Sergey K
Sergey K

Reputation: 4114

I think is the better to avoid if in the view, You should avoid business(Model) or application(Controller) logic in the view in your example you can create different Partial View for display some think depend on user role and in the Controller put the logic for what view you need to show

Upvotes: 0

Wojtek Turowicz
Wojtek Turowicz

Reputation: 4131

Basically every View should display whats passed in ViewModel. If ViewModel is not enough, then I would look for a way of improving the ViewModel creation itself, not the View's logic.

All conditionals CAN be evaluated upon ViewModel creation.

Of course, It all depends on how much custom-logic in View does Your project organization tolerates.

Upvotes: 0

Related Questions