首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使参数的ModelBinder返回null?

如何使参数的ModelBinder返回null?
EN

Stack Overflow用户
提问于 2012-07-14 01:49:31
回答 6查看 2.3K关注 0票数 4

我有一个POCO,我正在使用它作为MVC3中一个操作的参数。如下所示:

我的类型

代码语言:javascript
复制
public class SearchData
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
    public string Property3 { get; set; }
}

我的行动

代码语言:javascript
复制
public ActionResult Index(SearchData query)
{
    // I'd like to be able to do this
    if (query == null)
    {
        // do something
    }
}

目前,query作为SearchData的一个实例传递,所有属性都作为null传递。我更喜欢为query获得一个null,这样我就可以像上面的代码一样执行null检查。

我可以随时查看ModelBinder.Any()ModelBinder中的各种键,以查看它是否具有query的任何属性,但我不想使用反射来遍历query的属性。此外,如果query是我唯一的参数,我只能使用ModelBinder.Any()检查。一旦我添加了额外的参数,这个功能就会中断。

使用MVC3中当前的模型绑定功能,是否有可能将POCO参数返回null的行为返回给操作?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2012-07-14 01:54:47

为此,您需要实现一个自定义的模型绑定器。您可以只扩展DefaultModelBinder

代码语言:javascript
复制
public override object BindModel(
    ControllerContext controllerContext, 
    ModelBindingContext bindingContext)
{
    object model = base.BindModel(controllerContext, bindingCOntext);
    if (/* test for empty properties, or some other state */)
    {
        return null;
    }

    return model;
}

具体实现

这是绑定器的实际实现,如果所有属性都为null,则该绑定器将为模型返回null。

代码语言:javascript
复制
/// <summary>
/// Model binder that will return null if all of the properties on a bound model come back as null
/// It inherits from DefaultModelBinder because it uses the default model binding functionality.
/// This implementation also needs to specifically have IModelBinder on it too, otherwise it wont get picked up as a Binder
/// </summary>
public class SearchDataModelBinder : DefaultModelBinder, IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        // use the default model binding functionality to build a model, we'll look at each property below
        object model = base.BindModel(controllerContext, bindingContext);

        // loop through every property for the model in the metadata
        foreach (ModelMetadata property in bindingContext.PropertyMetadata.Values)
        {
            // get the value of this property on the model
            var value = bindingContext.ModelType.GetProperty(property.PropertyName).GetValue(model, null);

            // if any property is not null, then we will want the model that the default model binder created
            if (value != null)
                return model;
        }

        // if we're here then there were either no properties or the properties were all null
        return null;
    }
}

将其作为活页夹添加到global.asax中

代码语言:javascript
复制
protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    ModelBinders.Binders.Add(typeof(SearchData), new SearchDataModelBinder());
    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);

    MvcHandler.DisableMvcResponseHeader = true;
}
票数 6
EN

Stack Overflow用户

发布于 2012-07-14 01:55:31

在路线中尝试

代码语言:javascript
复制
new { controller = "Articles", action = "Index", query = UrlParameter.Optional }
票数 1
EN

Stack Overflow用户

发布于 2016-01-19 13:46:37

将自定义模型绑定器实现为参数上的属性。

注意:模型上的所有属性都必须为空

这里的

  1. 就是上面的ModelBinderClass

公共类模型: DefaultModelBinder { public override object NullModelBinder (controllerContext controllerContext,ModelBindingContext bindingContext) { //使用默认的模型绑定功能来构建模型,我们将查看object NullModelBinder= base.BindModel(controllerContext,bindingContext)下面的每个属性;//遍历元数据中模型的每个属性( bindingContext.ModelType.GetProperty(property.PropertyName).GetValue(model,中的ModelMetadata属性){ //在模型上获取该属性的值var value =bindingContext.PropertyMetadata.Values null);//如果有任何属性不为null,则需要默认模型绑定器创建的模型if (value != null)返回模型;} //如果我们在这里,那么要么没有属性,要么属性都是null返回null;}}

  • 创建属性

公共类NullModelAttribute : CustomModelBinderAttribute { public override IModelBinder GetBinder() { return new NullModelBinder();}}

  • 在控制器方法上使用特性

公共ActionResult索引(NullModel SearchData查询){ //我希望能够在(query == null) { //执行某些操作} }的情况下执行此操作

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11475684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档