I have a custom structure called TimeOfDay , which is used in the view model as follows:
public class MyViewModel { public TimeOfDay TimeOfDay { get; set; } }
I created a custom TimeOfDayModelBinder called TimeOfDayModelBinder and registered it in Global.asax.cs as follows:
ModelBinders.Binders.Add(typeof(TimeOfDay), new TimeOfDayModelBinder());
And everything works fine. However, if I change my look at this model:
public class MyViewModel { public TimeOfDay? TimeOfDay { get; set; }
My custom mediator is no longer called. I know that the property is no longer a TimeOfDay type, but Nullable is different. Does this mean that I have to add my own model binding twice to Global.asax.cs as follows:
ModelBinders.Binders.Add(typeof(TimeOfDay), new TimeOfDayModelBinder()); ModelBinders.Binders.Add(typeof(TimeOfDay?), new TimeOfDayModelBinder());
It works, but I just don't like it. Is this really necessary in order to treat my type as nullable, or is there something I am missing?
RenΓ©
source share