views:

63

answers:

3

I have an asp.net mvc application and I have a page for presenting meteo information. It looks like:

Temperature for today is 34-35 degrees

For this

34-35 degrees

, I have a method that assure that the text will be in format

[Number][Dot][Number]

called AssureCorrectDegressFormat(). Now I am asking where is it suited the best. Untill now I was calling it from the view, smth like this:

<%=SafeData.AssureCorrectDegressFormat(DV.TheDegreeString)%>

But as I think the view is intended only to display data , not to call some methods in order to operate with thise literals. I moved my class SafeData to the Core of my application, and I pass to the view the DTO that has already called this method and obtained the right data for displaying. I am interested in your opinions about this, where is the best place to put this class, maybe in Infrastructure layer and where to call it, now I call itr from my services . I forgot to say that I am using a DDD aproach.

+1  A: 

Formatting the display of data in the View seems fine to me. You wouldn't think twice about about putting this on your View:

<%= Model.MyDate.ToString("f") %>

would you? The principal is the same.

MikeB
+4  A: 

Formatting, from my perspective, is a view-related function and so it should be called in the view. As to the code that actually does the formatting, I might create an HtmlHelper extension to handle the formatting. That way I could use it wherever I wanted, but have the code in only one place.

 <%= Html.ShowDegrees( DV.TheDegreeString ) %>
tvanfosson
HtmlHelpers have the added benefit of reducing angst among developers when it comes to formatting methods appearing in the View.
MikeB
A: 

If it's a very simple formatting or calculation, I just put it in the view. If it's any more complex than that, I will put a method in the ViewModel. If it is complex and can be used many places, I will create an HtmlHelper for it.

Robert Harvey