Binding and Converting DateTime properties on all Models with ASP.NET MVC 3
I am just wondering if anyone has a method of binding DateTime properties of Models so that they are displayed in Views in the users selected timezone, but the Model uses them as UTC for storage in the database.
I am using ASP.NET MVC 3 with a SQL Server 2005 database but this will soon be upgraded to 2008 and will mean I can use the new datetime types if that helps with a solution.
I am using code from this post and this works well in binding date and time infomation posted from the View, and I assume with a little bit of extra code, I can easily convert the data to UTC.
So I guess I have 2 options that I need help with:
- Custom Renderer
Is there a way of overriding the object that renders the data to the View so that a UTC date is converted to the users timezone when displaying the View, then the custom开发者_开发百科 Binder I already have would convert it back for storage in the DB.
- Is there a better way that I am not aware of?
This is a feature that is available on many sites (I already use it on the first version of this site I did in straight ASP.NET), but before I went and used the same methods again (I just had a static function I called when displaying the information and then a similar function call before saving), just wanted to know if someone had a better solution.
Thanks Mark
You can use Html.EditorFor and Html.DisplayFor with customized template for DateTime. You will still need custom binder tho.
精彩评论