C# 未执行自定义模型绑定

C# 未执行自定义模型绑定,c#,.net,asp.net-mvc,model,C#,.net,Asp.net Mvc,Model,我有两个自定义活页夹来获取DateTime和DateTime?来绑定 我正在Global.asax中注册它们,比如: public class MvcApplication : HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilt

我有两个自定义活页夹来获取
DateTime
DateTime?
来绑定

我正在
Global.asax中注册它们,比如:

public class MvcApplication : HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        ModelBinders.Binders[typeof (DateTime)] = new DateTimeModelBinder();
        //ModelBinders.Binders[typeof (DateTime?)] = new NullableDateTimeModelBinder();

        //ModelBinders.Binders.Add(typeof (DateTime), new DateTimeModelBinder());
        //ModelBinders.Binders.Add(typeof(DateTime?), new NullableDateTimeModelBinder());
    }
}
我也试过这样做:

ModelBinders.Binders.Add(typeof (DateTime), new DateTimeModelBinder());
ModelBinders.Binders.Add(typeof(DateTime?), new NullableDateTimeModelBinder());
然而,他们从未被击中

模型绑定类在此处(从Internet上窃取):

在这里:

public class NullableDateTimeModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

        if (string.IsNullOrWhiteSpace(value.AttemptedValue))
        {
            return null;
        }

        DateTime dateTime;

        var isDate = DateTime.TryParse(value.AttemptedValue, Thread.CurrentThread.CurrentUICulture, DateTimeStyles.None, out dateTime);

        if (!isDate)
        {
            bindingContext.ModelState.AddModelError(bindingContext.ModelName, $"Cannot convert {value.AttemptedValue} to a DateTime.");
            return DateTime.UtcNow;
        }

        return dateTime;
    }
}

我错过了什么?

你能分享你的模型活页夹课程吗?你是从DefaultModelBinder继承的吗?也许这是可以帮助你的@umutözkan,这就是我所关注的帖子。但仍然没有击中装订机。
public class NullableDateTimeModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

        if (string.IsNullOrWhiteSpace(value.AttemptedValue))
        {
            return null;
        }

        DateTime dateTime;

        var isDate = DateTime.TryParse(value.AttemptedValue, Thread.CurrentThread.CurrentUICulture, DateTimeStyles.None, out dateTime);

        if (!isDate)
        {
            bindingContext.ModelState.AddModelError(bindingContext.ModelName, $"Cannot convert {value.AttemptedValue} to a DateTime.");
            return DateTime.UtcNow;
        }

        return dateTime;
    }
}