views:

382

answers:

2

In our standard web forms ASP.NET solutions we typically have a range of user controls within the master page and determine whether they display or not within their code behind. What is the best approach within ASP.NET MVC to achieve the same goal?

You could obviously put if statements within the master page or the partial view but that strikes me as messy and breaks the principle of keeping business logic out of the view. It also requires either putting the necessary information into all view models, or inheriting from a base controller which seems like a lot of messing about for something so simple.

I was thinking about using RenderAction and returning a totally blank view to prevent any output - is that a good pattern?

+2  A: 

breaks the principle of keeping business logic out of the view

It is not business logic. It is presentation logic when you determine whether to display something or not. It is okay to have it there.

You can make decision whether to display something or not and set up a few flags in the model (you can make BaseModel or MasterModel for example). Then your master views, partial views themselves or HTML helpers will perform conditional rendering based on these flags.

As to the clean separation of concerns, yes, the WebForms could seemingly do it but it was rather a huge abstraction of the underlying mechanisms. Often it results in having business logic in the code-behind, meaning, in the presentation layer where business logic belongs no more than it belongs to the views.

Developer Art
I second. Got a bunch of "RenderPartialIfNotNull("Foo", Model.Something)" calls.
Arnis L.
Sorry, yes, I meant UI logic not business logic
Rob West
A: 

You could avoid the messy "If" statements in view by creating a Html extension method as mentioned in my recent post http://www.rajeeshcv.com/2010/01/asp-net-mvc-conditional-rendering-partial-views/

Thanks,

Rajeesh

Rajeesh