breaking datetime into constituent parts ASP.NET MVC form

Posted by nick on Stack Overflow See other posts from Stack Overflow or by nick
Published on 2010-05-24T10:26:30Z Indexed on 2010/05/24 10:31 UTC
Read the original article Hit count: 199

hi guys,

i have searched the web relentlessly for this and have not found anything - which is surprising because i would think it is such a common scenario!

Basically, on my model i have a DateTime field which i wish the user to populate through a form. I am using the Html helper to render all other parts of the form (along with validation)

So this question is in two parts...

Html Helper

Firstly, is there any way to use the Html helper to split the DateTime field to be rendered as the three constituent parts of a date: day, month, year (since i do not care about the time part). This could be rendered as text boxes, drop down lists or a combination of both.

Model Binding

And then when the form is posted, what is the best approach for binding back up to the model? I have seen Scott Hanselmann's solution to this, but it seems a little bloated for what i need - i was hoping for a slightly more elegant solution. Is it recommended to extend DefaultModelBinder and set that as default binder (since all dates would be handled in this way) or write a class that implements IModelBionder and set it as the default binder for the DateTime type?

Thanks for all the help in advance :-) i'm loving MVC but it's infuriating me that something so trivial is causing so much headaches!

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about datetime