在绑定之前使用 MVC ModelBinders 过滤 post 值
我需要在从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哇,绑定上下文有一个 ModelName 属性,它为您提供前缀(对于列表项)。使用它可以让我获得原始表单值:
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: