views:

515

answers:

6

In looking at samples of ASP.NET MVC sites, I'm seeing quite a bit of examples with embedded logic in the views, e.g.:

<% if (customerIsAllowed)
   { %>

   <p>nnn</p>
   <p>nnn</p>
   <p>nnn</p>
   <p>nnn</p>
   <p>nnn</p>

<% }  else {%>

   <p>nnn</p>
   <p>nnn</p>
   <p>nnn</p>
   <p>nnn</p>
   <p>nnn</p>

<% } %>

Although this seems wrong to me since it is the kind of thing we were trying to get away from in ASP 3.0, I have even heard in some podcasts how "a little bit of logic in view is ok" since the rest of the MVC framework is taking care of the structure that we didn't have in ASP 3.0.

Are there any MVC conventions specifying what kind and how much logic is allowed in views?

+10  A: 

It depends on the reason for the logic. If the logic is choosing an alternate presentation based on some property passed to it by the controller, it is probably ok. This allows you some view reuse. Instead of having to recreate (and repeat) an entire view for each custom privilege, you can pass in some data that allows the view to be customized based on this privilege.

I think of this as a pragmatic balance between an idealized MVC and strict enforcement of DRY (don't repeat yourself). In some situations it is wiser to violate one or the other if you can't attain both easily. In the case where clearly the model and the basic view is the same, putting a little logic in the view to keep your views DRY is reasonable.

tvanfosson
Agreed. customerIsAllowed would be populated based on some domain logic in the Model, which makes this View logic ok IMO.
Crescent Fresh
Agree, the amount of logic is irrelevant to the separation of concerns. Keeping logic minimal in the views is a practical concern. But view logic belongs in the view.
Haacked
Agree. View logic belongs in the view. That is why I don't understand the harsh criticism of using Codebehind files in MVC. I understand that it can be misused, but some simple properties or conditional logic is sometimes easier to write in the codebehind than in "<%" blocks
Trevor de Koekkoek
+1  A: 

Here's another way to think about it. Presentation logic goes in the view. Business processing logic goes in the controller, and data validation goes in the model. But what goes where should ultimately be guidance and not religion:)

barneytron
-1. This is a common misconception. Business logic does not belong in the controller. Business logic is part of the model. The controllers job is to access appropriate parts of the model and use what if finds to decide which view to display.
Jack Ryan
I agree that ideally, there should be no business logic (an over-loaded term) in the controller. But in practice, a small portion of business logic winds up in the controller. My 2 cents:)
barneytron
+2  A: 

if the logic pertains to the format of the view, and does not result in changes to entities or data, then I think it is OK in the view.

Matthew
+1  A: 

42.

Just kidding :-)

There's no set answer to this, though a good separation of concerns is a generally accepted best practice. The debate around this can be pretty endless, and knowing the right way to do it for your particular project comes with experience I think and being able to notice "code smell" or things that don't feel right.

Jesse Taber
A: 

As long as the logic in the view is for the presentation (you could put it in the code behind file if you don't like it in the markup file) then it is ok. In your example the code / logic is for selecting a certain view part which is ok. The presentation is allowed to have logic it doesn't need to be simply HTML.

Andrei Rinea
A: 

I wrote a blog post about Asp.Net MVC best practices and keeping logic out of your views

Jag Reehal