Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 为什么可以';t为列表注册自定义模型活页夹<;int>;?_Asp.net Mvc_Asp.net Mvc 4_Model Binding - Fatal编程技术网

Asp.net mvc 为什么可以';t为列表注册自定义模型活页夹<;int>;?

Asp.net mvc 为什么可以';t为列表注册自定义模型活页夹<;int>;?,asp.net-mvc,asp.net-mvc-4,model-binding,Asp.net Mvc,Asp.net Mvc 4,Model Binding,我有一个动作看起来像 public ActionResult GetUsers(List<int> userIds) {//do stuff} 我用这行代码注册了这个活页夹 ModelBinders.Binders.Add(typeof(List<int>), new JsonBinder()); ModelBinders.Binders.Add(typeof(List),newjsonbinder()); 但是,从未调用JsonBinder。为什么会这样?我应该使

我有一个动作看起来像

public ActionResult GetUsers(List<int> userIds) {//do stuff}
我用这行代码注册了这个活页夹

ModelBinders.Binders.Add(typeof(List<int>), new JsonBinder());
ModelBinders.Binders.Add(typeof(List),newjsonbinder());

但是,从未调用JsonBinder。为什么会这样?我应该使用ValueProvider吗?

在global.asax中添加以下事件(或将代码添加到现有的应用程序\u BeginRequest处理程序):

然后,您可以在VS输出窗口中检查当前注册了哪些绑定。你可以看到这样的情况:

Binder for 'System.Web.HttpPostedFileBase': 'System.Web.Mvc.HttpPostedFileBaseModelBinder'
Binder for 'System.Byte[]': 'System.Web.Mvc.ByteArrayModelBinder'
Binder for 'System.Data.Linq.Binary': 'System.Web.Mvc.LinqBinaryModelBinder'
Binder for 'System.Threading.CancellationToken': 'System.Web.Mvc.CancellationTokenModelBinder'
您还可以检查是否有任何
ModelBinderProvider
可以选择活页夹提供程序,因为选择使用哪个模型活页夹的顺序如下:

  • 操作参数上的属性。请参阅的GetParameterValue方法

  • 从IModelBinderProvider返回的活页夹。请参见中的GetBinder方法

  • 绑定器在ModelBinders.Binders字典中全局注册

  • 在模型类型的
    [ModelBinder()]
    属性中定义的活页夹

  • DefaultModelBinder

  • 使用类似的方法检查BeginRequest事件中的模型绑定器提供程序:

    foreach (var binderprovider in ModelBinderProviders.BinderProviders)
    {
        System.Diagnostics.Trace.WriteLine(String.Format("Binder for '{0}'", binderprovider.ToString()));
    }
    
    此外,您可以尝试通过nuget添加,因为其中一个选项卡提供了有关用于控制器操作中每个参数的模型绑定器的信息


    希望这将有助于您跟踪模型绑定器未被使用的原因。

    在global.asax中添加以下事件(或将代码添加到现有的应用程序\u BeginRequest处理程序中):

    然后,您可以在VS输出窗口中检查当前注册了哪些绑定。你可以看到这样的情况:

    Binder for 'System.Web.HttpPostedFileBase': 'System.Web.Mvc.HttpPostedFileBaseModelBinder'
    Binder for 'System.Byte[]': 'System.Web.Mvc.ByteArrayModelBinder'
    Binder for 'System.Data.Linq.Binary': 'System.Web.Mvc.LinqBinaryModelBinder'
    Binder for 'System.Threading.CancellationToken': 'System.Web.Mvc.CancellationTokenModelBinder'
    
    您还可以检查是否有任何
    ModelBinderProvider
    可以选择活页夹提供程序,因为选择使用哪个模型活页夹的顺序如下:

  • 操作参数上的属性。请参阅的GetParameterValue方法

  • 从IModelBinderProvider返回的活页夹。请参见中的GetBinder方法

  • 绑定器在ModelBinders.Binders字典中全局注册

  • 在模型类型的
    [ModelBinder()]
    属性中定义的活页夹

  • DefaultModelBinder

  • 使用类似的方法检查BeginRequest事件中的模型绑定器提供程序:

    foreach (var binderprovider in ModelBinderProviders.BinderProviders)
    {
        System.Diagnostics.Trace.WriteLine(String.Format("Binder for '{0}'", binderprovider.ToString()));
    }
    
    此外,您可以尝试通过nuget添加,因为其中一个选项卡提供了有关用于控制器操作中每个参数的模型绑定器的信息


    希望这将有助于您追踪模型绑定器未被使用的原因。

    您是否尝试过在操作方法中使用模型绑定器属性

    public ActionResult GetUsers([ModelBinder(typeof(JsonBinder))] List<int> userIds)
    
    public ActionResult GetUsers([ModelBinder(typeof(JsonBinder))]List userid)
    

    Ref:

    您是否尝试在操作方法中使用ModelBinder属性

    public ActionResult GetUsers([ModelBinder(typeof(JsonBinder))] List<int> userIds)
    
    public ActionResult GetUsers([ModelBinder(typeof(JsonBinder))]List userid)
    

    Ref:

    您是否检查过您的型号活页夹没有被类型
    列表覆盖?您可以尝试在global中的Application_Start事件结束时注册它。我不确定如何检查模型绑定器是否被覆盖。模型活页夹的注册在应用程序启动事件结束时完成。您是否检查过您的模型活页夹没有被类型
    列表覆盖?您可以尝试在global中的Application_Start事件结束时注册它。我不确定如何检查模型绑定器是否被覆盖。模型绑定器的注册在应用程序启动事件结束时完成。