15

I've been looking a lot recently as best practices within the ASP.NET MVC framework, specifically around sparation of concern. My question here is, if I have a property on a model, where is the best place to do some formatting on that property?

I've thought about a few options but am not sure which is best. The example uses a DateTime. Sorry this got a bit long.

Option 1: In the view: @Model.TheDate.String("{0:'blah'dd/MM/yyyy}") I know this isn't right because the format string shouldn't be here (what if I want to change the format throughout the whole app)

Option 2: Extension method used within the view: @Model.TheDate.DisplayItNicePlease() This kind of makes sense because the view is chosing the extension method to use for formatting and the viewmodel just exposes the date property to the view. Not sure if the view or view model should be responsible for formatting though.

Option 3: In an Html Helper method, i.e. something like Where should I place Declarative HTML helpers in ASP.NET MVC 3 so you could use:

@Html.DisplayDateNice(Model.TheDate)

Option 4: Extra property on the view model to format it and return a string e.g.:

public string TheDateStr
{
    get
    {
        return TheDate.DisplayItNicePlease();
    }
}

And then in the view you just have

@Model.TheDateStr

Fairly similar to option 3 only it keeps the view really, really simple - the view literally just outputs what is in the model and doesn't care about the format of the data.

Option 5: DisplayFormat attributes, e.g.: On the view model:

[DisplayFormat(DataFormatString = "{0:'blah'dd/MM/yyyy}")]
public DateTime TheDate { get; set; }

And then on the view:

@Html.DisplayFor(m => m.TheDate)

I like this apart from again the format string is not reused, so bring on option 5...

Option 6: Some kind of custom DisplayFormatAttribute e.g. NiceDateDisplayFormatAttribute. This seems to me like it might be the nicest solution and works well if you just want a simple string formatting. I think this gets more complicated if you want something more complicated, e.g showing relative time (Calculate relative time in C#), in which case maybe this is best being in an extension method/html helper.

There are probably other ways I'm not even aware of...

Community
  • 1
  • 1
Ian Routledge
  • 4,012
  • 1
  • 23
  • 28

2 Answers2

3

Either use an extension helper to format the type if this type formatting will be used a lot or add an extra property in the Model to return a formatted version of the original property.

Chris Snowden
  • 4,982
  • 1
  • 25
  • 34
  • OK, so you think the VM should be doing the formatting, not the view? – Ian Routledge Sep 23 '11 at 11:03
  • Yes, because then the same formatting function can be used multiple times in the View without repitition plus this keeps the Model processing in the Model leaving the View to just concentrate on the View part of MVC. – Chris Snowden Sep 23 '11 at 11:05
  • Thanks. So really, the answer is an extra property on the VM that returns the formatted string, but the actual formatting inside that property is done by an extension method so the formatting can be re-used throught the app. That makes sense.... – Ian Routledge Sep 23 '11 at 11:15
  • If I am using some kind of mapping layer, then should that formatting should be done there right? The VM is just getters and setters. Or is that an entirely different question? :S – Ian Routledge Sep 23 '11 at 11:17
  • Ian don't think too much. what u have already got is sufficient. and best practices vary from person to person on micro level and you will finally settle with something u r comfortable with. Too much standardization can kill productivity – Muhammad Adeel Zahid Sep 23 '11 at 12:00
  • @Ian, yes that sums up what I'd recommend generally. Use extensions for formatting functions that are used often and then extend your model for extra proprties as needed. Of course for a one off, putting it in the view will be fine but not good if repeated a lot. – Chris Snowden Sep 23 '11 at 13:10
1

For a single view, I personally use option 5: putting it in the view model. If date format's being used for multiple pages, you can create a html helper extension for formatting the date. So in the view, you'd have something like

@Model.TheDate.ToDateString()

But overall, i still prefer option5 because i create viewmodels for everypage, and just throw a DisplayFormat attribute on it.

mnsr
  • 12,337
  • 4
  • 53
  • 79