KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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 TheD
Tags (comma-separated)
Save Edits
Cancel