title: Where is the best place to format Model properties in ASP.NET MVC(3)?
body: <p>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?</p>
  
  <p>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.</p>
  
  <p><strong>Option 1:</strong>
  In the view: <code>@Model.TheDate.String("{0:'blah'dd/MM/yyyy}")</code>
  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)</p>
  
  <p><strong>Option 2:</strong>
  Extension method used within the view: <code>@Model.TheDate.DisplayItNicePlease()</code>
  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.</p>
  
  <p><strong>Option 3:</strong>
  In an Html Helper method, i.e. something like <a href="https://stackoverflow.com/questions/3981175/where-should-i-place-declarative-html-helpers-in-asp-net-mvc-3">Where should I place Declarative HTML helpers in ASP.NET MVC 3</a> so you could use:</p>
  
  <pre><code>@Html.DisplayDateNice(Model.TheDate)
  </code></pre>
  
  <p><strong>Option 4:</strong>
  Extra property on the view model to format it and return a string e.g.:</p>
  
  <pre><code>public string TheDateStr
  {
      get
      {
          return TheDate.DisplayItNicePlease();
      }
  }
  </code></pre>
  
  <p>And then in the view you just have</p>
  
  <pre><code>@Model.TheDateStr
  </code></pre>
  
  <p>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.</p>
  
  <p><strong>Option 5:</strong>
  DisplayFormat attributes, e.g.:
  On the view model:</p>
  
  <pre><code>[DisplayFormat(DataFormatString = "{0:'blah'dd/MM/yyyy}")]
  public DateTime TheDate { get; set; }
  </code></pre>
  
  <p>And then on the view:</p>
  
  <pre><code>@Html.DisplayFor(m =&gt; m.TheDate)
  </code></pre>
  
  <p>I like this apart from again the format string is not reused, so bring on option 5...</p>
  
  <p><strong>Option 6:</strong>
  Some kind of custom <code>DisplayFormatAttribute</code> e.g. <code>NiceDateDisplayFormatAttribute</code>.
  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 (<a href="https://stackoverflow.com/questions/11/how-do-i-calculate-relative-time">Calculate relative time in C#</a>), in which case maybe this is best being in an extension method/html helper.</p>
  
  <p>There are probably other ways I'm not even aware of...</p>