在绑定之前使用 MVC ModelBinders 过滤 post 值

发布于 2024-10-11 15:28:17 字数 1429 浏览 2 评论 0原文

我需要在从 MVC2 中的 POST 数据绑定一些值之前过滤掉它们。不幸的是,我无法更改客户端代码,该代码有时会为要映射为十进制的表单值传递“N/A”?类型。需要发生的是,如果“N/A”是 POST 值,则在绑定/验证之前将其清空。

我整个上午都在尝试使用扩展 DefaultModelBinder 的 ModelBinder 来使其工作:

public class DecimalFilterBinder : DefaultModelBinder
{
    protected override void BindProperty(ControllerContext controllerContext,
        ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
    {
        if (propertyDescriptor.PropertyType == typeof(decimal?))
        {
            var model = bindingContext.Model;
            PropertyInfo property = model.GetType().GetProperty(propertyDescriptor.Name);
            var httpRequest = controllerContext.RequestContext.HttpContext.Request;
            if (httpRequest.Form[propertyDescriptor.Name] == "-" ||
                httpRequest.Form[propertyDescriptor.Name] == "N/A")
            {
                property.SetValue(model, null, null);
            }
            else
            {
                base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
            }
        }
        else
        {
            base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
        }
    }
}

我遇到的问题是我不知道如何访问列表中最初发布的值。我不能只是 Form[propertyDescriptor.Name] 因为它包含在表单中的列表项中(因此输入实际上是 Values[0].Property1,对于例子)。我将模型绑定器连接到 global.asax 并运行良好,我只是不知道如何在默认绑定发生之前获取原始表单值以将其过滤为空字符串。

I need to filter out some values before they are bound from POST data in MVC2. Unfortunately i can't change the client side code which is sometimes passing "N/A" for a form value that is to be mapped into decimal? type. What needs to happen is if "N/A" is the POST value blank it out before it's bound/validated.

I've tried all morning to get it working using a ModelBinder that extends the DefaultModelBinder:

public class DecimalFilterBinder : DefaultModelBinder
{
    protected override void BindProperty(ControllerContext controllerContext,
        ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
    {
        if (propertyDescriptor.PropertyType == typeof(decimal?))
        {
            var model = bindingContext.Model;
            PropertyInfo property = model.GetType().GetProperty(propertyDescriptor.Name);
            var httpRequest = controllerContext.RequestContext.HttpContext.Request;
            if (httpRequest.Form[propertyDescriptor.Name] == "-" ||
                httpRequest.Form[propertyDescriptor.Name] == "N/A")
            {
                property.SetValue(model, null, null);
            }
            else
            {
                base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
            }
        }
        else
        {
            base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
        }
    }
}

The problem I'm having is I don't know how to get access to the originally posted value when it is within a list. I can't just to Form[propertyDescriptor.Name] because it's contained within a list item in the form (so the input is really Values[0].Property1, for example). I have the model binder hooked up in global.asax and running fine, I just don't know how to get a hold of the original form value to filter it out to an empty string before the default binding happens.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不如归去 2024-10-18 15:28:17

哇,绑定上下文有一个 ModelName 属性,它为您提供前缀(对于列表项)。使用它可以让我获得原始表单值:

...
var httpRequest = controllerContext.RequestContext.HttpContext.Request;
if (httpRequest.Form[bindingContext.ModelName + propertyDescriptor.Name] == "-" ||
    httpRequest.Form[bindingContext.ModelName + propertyDescriptor.Name] == "N/a")
{
    property.SetValue(model, null, null);
}
else
{
    base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}
...

Wow, the bindingContext has a ModelName property which gives you the prefix (for the list item). Using that lets me get the original form value:

...
var httpRequest = controllerContext.RequestContext.HttpContext.Request;
if (httpRequest.Form[bindingContext.ModelName + propertyDescriptor.Name] == "-" ||
    httpRequest.Form[bindingContext.ModelName + propertyDescriptor.Name] == "N/a")
{
    property.SetValue(model, null, null);
}
else
{
    base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文