What’s the best way to edit a nullable DateTime property in asp.net MVC 3 in separate text boxes or drop down lists?
I want to split the DateTime into day, month and year – display the month as a dropdown list. The property is optional, so it should be null if no date is supplied. I assume that I have to use a custom model binder like this for MVC 2: http://www.hanselman.com/blog/SplittingDateTimeUnitTestingASPNETMVCCustomModelBinders.aspx – but this doesn’t seem to work with optional dates and server side validation isn’t triggered properly (e.g. entering text for the date). I don’t want to use jQuery here.
This should be pretty straightforward, quite common use case – right? Thanks for pointing me in the right direction.
Actually the common use case these days would be to use jQuery and a single textbox, but if you really want to use dropdowns you would need to have separate properties in your model for day/month/year and do the conversion to a date in your controller action.
You could do the separation in the view or an html helper that generates javascript and a hidden field, but that’s getting to be too much logic for a view.