tags:

views:

163

answers:

3

Stackoverflow is built on MVC and does a bunch of simple but nice things with dates and numbers (answer counts, votes, etc...). I'm assuming this is all done with a couple of static helper classes.

My question is should the view call these helper objects or should the controller call these helper objects and embed the result into the ViewData?

  1. Controller Populates View Data

    • Controller Loads Model/Data
    • Controller uses DateHelper static method to convert date to friendly string
    • Friendly date string is provided to view via ViewData
  2. Controller Populates View Data with Model and View calls DateHelper

    • Controller Loads Model/Data
    • Controller provides model to view via ViewData
    • When ViewData is binding to HTML it calls DateHelper static method

I suspect number 1 is the right way to go, but it seems a bit messy in that you can take a List of models and those are easy to loop over in the View. If you process and generate a bunch of friendly strings based on a set of model instances and the model doesn't have a place for those to be attached to a specific instance you end up sending two different lists of data to ViewData. Right?

Your thoughts are appreciated.

+2  A: 

I think I would prefer to use the helper in the view. In my opinion, how a date gets rendered is a view centric thing. The controller just knows that the view needs to display it.

Rob
+7  A: 

Depending on who you ask, you'll get different answers. I personally like option #2 as I want the ViewData to be raw data and choose how it is rendered in the View. To me, that's perfectly fine to do that in the view as it's a view concern.

However, I know people on my own team that disagree with me, suggesting that the controller is returning a presentation model, not just a model (I do agree with that as well) and it should handle all these conversions before sending the model to the view. The view in this case should be super dumb.

I don't think there's one absolute right choice for everybody in this case. I'd say pick one or the other based on your experience and stick to it.

Haacked
+1  A: 

I'm all for option 2. I'd say it's a formatting issue, which is, in my opinion, for the view to worry about.

Jeff Sheldon