Monday, July 11, 2016

Adding Display Formatting to ASP.NET MVC ViewModels

If you have a need to add formatting to your ASP.NET MVC screens, chances are that you want to avoid doing this in your Razor Views since it makes the views more difficult to maintain especially when you use Scaffolding.

Fortunately, you can add Display Formatting very easily to your ViewModels using this DataAnnotation attribute (https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displayformatattribute(v=vs.110).aspx):

[DisplayFormat(DataFormatString = "{0:MM / dd / yyyy}")]


That is all that is needed to get something like Date Formatting added to your ViewModel!

No comments:

Post a Comment