0

I have a scenario where I have a view that can be shown in Mode A or Mode B depending on the characteristic of the user, lets say User A can see View Mode A and User B can see View Mode B. Should you have two separate views for this scenario where the logic is kept within the controller (even though the views are mostly similar, only View Mode B hides some fields) or is it okay to have View Logic such as

@if (Usertype == "A"){ then display XYZ } else { display ABC }

3
  • it depends on how different the information shown to the users is. if its very different, then you'll have 500 if/else blocks in your view which i dont care for. Instead, use partial views and write one if/else block. if user is type A then show them this partial view, if user is type B then show them a diff partial view. Keep in mind you say the views are similar now but this often changes. This question is pretty opinion based IMO Commented Dec 17, 2018 at 13:38
  • @GregH Gotcha. So in my instance because the view is very unlikely to change it is best to have one view with conditionals to hide/show fields. If its a matter of opinion great, just wanted to see if there was a specific standard that most follow. Commented Dec 17, 2018 at 13:46
  • 1
    yep as long as there arent a ton of conditionals showing and hiding fields. strive for simplicity Commented Dec 17, 2018 at 13:49

3 Answers 3

2

If logic is related to just hide/show some of fields then keep single view and apply condition to render particular things on page. Reason is maintaining single view in this case is easy in long term future, if any new UserType(User) can be added in system.

Sign up to request clarification or add additional context in comments.

Comments

2

I would also look at the possibility of these diverging visually. If there is even the slightest chance where B may need something different added to it that would end up altering the layout, then use separate views and let the controller contain the logic.

Additionally, this lets you put that logic test into a unit test at some point. Putting the logic in the controller also helps ensure that if there is a change to the data layer that could break this condition check by failing the build. If you put that logic into the view, you're not going to get that safety net and you'll end up with runtime errors after they are released instead of a nice development error that's easy to fix.

Comments

0

My preference is to have as little code in Views as possible. I try to keep code in views to looping over collections from the model and selecting properties from the model. Why? Although not impossible, testing all the logic in a view is difficult and seldom kept up to date. Every statement is a potential YSOD in a view.

Make the decision in your controller if possible and return a named view. You can write unit tests on your controller to verify what you expect is actually happening.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.