ASP.NET MVC3将POST值绑定到“对象”类型会使该对象成为System.String [],为什么?

问题描述

| 因此,我在我的网站上有一个搜索表单,该表单以
Search.Value
作为参数,项目中有一个名为
Search
的类,其属性为
Value
,即is3ѭ。当值由模型绑定器绑定时,无论我传递的是单个数字,单个单词还是其他任何值,对象始终为4。 有人可以向我解释为什么会这样,还是我可以让它停止这样做呢? 这是代码:
// Search.cs
public sealed class Search {
    private object _Value = null;

    public object Value {
        get {
            return this._Value;
        }
        set {
            if (value != null) {
                this._Value = value;

                this.IsInt32 = (this._Value is Int32);
                this.IsString = (this._Value is String);
            };
        }
    }

    public bool IsInt32 { get; set; }
    public bool IsString { get; set; }
}

// SearchController.cs
[HttpPost]
public ActionResult List(
    [Bind(Prefix = \"Search\",Include = \"Value\")] Search Search) {
    return this.View();
}

// Form HTML
<form action=\"/Administration/Search\" method=\"post\">
    @Html.TextBox(\"Search.Value\",Model.Search.Value,new { type = \"search\",placeholder = \"Search\",size = 48,required = string.Empty })
    <input type=\"submit\" value=\"&#8981;\" />
</form>
更新 根据@Darin的建议和示例,我制作了一个自定义的活页夹,到目前为止,它似乎仍在工作。如果有其他人遇到,这里是代码,请根据需要进行修改:
public class SearchModelBinder : DefaultModelBinder {
    public override object BindModel(
        ControllerContext ControllerContext,ModelBindingContext BindingContext) {
        if (BindingContext == null) {
            throw new ArgumentNullException(\"BindingContext\");
        };

        ValueProviderResult ValueResult = BindingContext.ValueProvider.GetValue(BindingContext.ModelName + \".Value\");

        if (ValueResult == null) {
            return (null);
        };

        string Value = ValueResult.AttemptedValue;

        if (String.IsNullOrEmpty(Value)) {
            return (null);
        };

        int Int;

        if (int.TryParse(Value,out Int)) {
            return new Search {
                Value = Convert.ToInt32(Value)
            };
        };

        long Long;

        if (long.TryParse(Value,out Long)) {
            return new Search {
                Value = Convert.ToInt64(Value)
            };
        };

        return new Search {
            Value = Value
        };
    }
}
    

解决方法

        不知道您要达到什么目的,为什么要使用
object
,...但是您可以为此
Search
模型编写自定义模型绑定程序。该模型绑定器将直接将请求中发送的参数分配给
Value
属性:
public class SearchModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext)
    {
        var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + \".Value\");
        if (value != null)
        {
            return new Search
            {
                Value = value.AttemptedValue
            };
        }
        return base.BindModel(controllerContext,bindingContext);
    }
}
您可以在
Global.asax
中注册:
ModelBinders.Binders.Add(typeof(Search),new SearchModelBinder());
    

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...