Search code examples
c#asp.net-mvc-3custom-model-binder

Custom model binder not called when type is nullable


I have a custom struct called TimeOfDay which is used in a view model like this:

public class MyViewModel
{
  public TimeOfDay TimeOfDay { get; set; }
}

I have created a custom model binder called TimeOfDayModelBinder and registered it in Global.asax.cs like this:

ModelBinders.Binders.Add(typeof(TimeOfDay), new TimeOfDayModelBinder());

And everything works great. However, if I change my view model to this:

public class MyViewModel
{
  public TimeOfDay? TimeOfDay { get; set; } // Now nullable!
}

My custom model binder is no longer called. I know that the property is no longer a type of TimeOfDay, but a Nullable which is different. So does this mean I should add my custom model binder twice in Global.asax.cs like this:

ModelBinders.Binders.Add(typeof(TimeOfDay), new TimeOfDayModelBinder());
ModelBinders.Binders.Add(typeof(TimeOfDay?), new TimeOfDayModelBinder());

It works, but there's just something I don't like about it. Is this really necessary to handle my type as nullable, or is there something I'm missing?


Solution

  • Per @LukeH's comment, it seems it is necessary. I guess that makes sense too, as TimeOfDay and Nullable<TimeOfDay> really is two different types in the CLR. So I guess I have to live with it. :-)