Is formatting strings in a View evil?

Yesterday, I ran into an interesting post by Chris Brandsma about his personal rules when dealing with Views in ASP.NET MVC. While he sure has some valid points, I don’t agree with everything he says in that post. Apparently, I’m not the only one since Jeff Putz beat me to it and made a post on his blog about the same doubts that I have about Chris’ ideas.

The first thing I don’t agree with is that Views shouldn’t do any formatting or parsing. According to Chris, DateTime values should be formatted by the ViewModel and passed to the View as strings. The problem I have with this way of working is: where do you draw the line? Does that mean you also have to let the ViewModel format decimal values representing a price and pass that to the View as a string? Ultimately, it implies that a ViewModel should only have strings for properties. As Jeff points out, this would also mean HtmlHelpers are to be avoided as well. I don’t think there’s anything wrong with letting the View format a DateTime value. Making calculations is something else of course.

The second thing I don’t agree with is that the ViewModel should also pass in the CSS classes of the HTML elements. For me, that’s at least one bridge too far. I agree that you shouldn’t put business logic in the View, but putting markup elements in the ViewModel shouldn’t be done either.

Of course, these are just my virtual two cents on the subject.

This entry was posted in Development and tagged . Bookmark the permalink.

One Response to Is formatting strings in a View evil?

  1. Pingback: Tweets that mention Is formatting strings in a View evil?Kristof Claes | Kristof Claes -- Topsy.com