如何筛选使用自定义的模型绑定表单数据 [英] How to filter form data with custom model binder

查看:72
本文介绍了如何筛选使用自定义的模型绑定表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆的,其中输入货币值的形式和我希望他们能够进入1,234.56 $。默认情况下,该模型粘合剂不会解析为十进制。

我在想什么做的是创造粘结剂继承DefaultModelBinder一个自定义模式,覆盖BindProperty方法,检查属性描述符类型是小数,如果是,刚剥离出$,并从数值。

这是最好的办法?

code:

 公共类CustomModelBinder:DefaultModelBinder
{
 保护覆盖无效BindProperty(ControllerContext controllerContext,ModelBindingContext的BindingContext,System.ComponentModel.PropertyDescriptor的PropertyDescriptor)
 {
  如果(propertyDescriptor.PropertyType == typeof运算(十进制)|| propertyDescriptor.PropertyType == typeof运算(十进制?))
  {
   VAR为newValue = Regex.Replace(bindingContext.ValueProvider [propertyDescriptor.Name] .AttemptedValue,@[$,],,RegexOptions.Compiled);
   bindingContext.ValueProvider [propertyDescriptor.Name] =新ValueProviderResult(为newValue,为newValue,bindingContext.ValueProvider [propertyDescriptor.Name]。文化);
  }  base.BindProperty(controllerContext,BindingContext中,PropertyDescriptor的);
 }
}

更新

这是我落得这样做:

 公共类CustomModelBinder:DataAnnotationsModelBinder
{
保护覆盖无效BindProperty(ControllerContext controllerContext,ModelBindingContext的BindingContext,System.ComponentModel.PropertyDescriptor的PropertyDescriptor)
{
如果(propertyDescriptor.PropertyType == typeof运算(十进制)|| propertyDescriptor.PropertyType == typeof运算(十进制?))
{
十进制为newValue;
decimal.TryParse(bindingContext.ValueProvider [propertyDescriptor.Name] .AttemptedValue,NumberStyles.Currency,空出为newValue);
bindingContext.ValueProvider [propertyDescriptor.Name] =新ValueProviderResult(为newValue,newValue.ToString(),bindingContext.ValueProvider [propertyDescriptor.Name]。文化);
}
base.BindProperty(controllerContext,BindingContext中,PropertyDescriptor的);
}
}


解决方案

这是合理的,做的粘合剂。但是,我认为, Decimal.Parse 与货币格式提供商或编号样式(的see的文档)会比剥$,并呼吁更可靠。首先,它会处理非美货币,这可能是你的问题的一天。

I have a bunch of forms where currency values are entered and I want them to be able to enter "$1,234.56". By default, the model binders won't parse that into a decimal.

What I am thinking of doing is creating a custom model binder the inherits DefaultModelBinder, override the BindProperty method, check if the property descriptor type is decimal and if it is, just strip out the $ and , from the values.

Is this the best approach?

Code:

public class CustomModelBinder : DefaultModelBinder
{
 protected override void BindProperty( ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor )
 {
  if( propertyDescriptor.PropertyType == typeof( decimal ) || propertyDescriptor.PropertyType == typeof( decimal? ) )
  {
   var newValue = Regex.Replace( bindingContext.ValueProvider[propertyDescriptor.Name].AttemptedValue, @"[$,]", "", RegexOptions.Compiled );
   bindingContext.ValueProvider[propertyDescriptor.Name] = new ValueProviderResult( newValue, newValue, bindingContext.ValueProvider[propertyDescriptor.Name].Culture );
  }

  base.BindProperty( controllerContext, bindingContext, propertyDescriptor );
 }
}

Update

This is what I ended up doing:

public class CustomModelBinder : DataAnnotationsModelBinder
{
	protected override void BindProperty( ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor )
	{
		if( propertyDescriptor.PropertyType == typeof( decimal ) || propertyDescriptor.PropertyType == typeof( decimal? ) )
		{
			decimal newValue;
			decimal.TryParse( bindingContext.ValueProvider[propertyDescriptor.Name].AttemptedValue, NumberStyles.Currency, null, out newValue );
			bindingContext.ValueProvider[propertyDescriptor.Name] = new ValueProviderResult( newValue, newValue.ToString(), bindingContext.ValueProvider[propertyDescriptor.Name].Culture );
		}
		base.BindProperty( controllerContext, bindingContext, propertyDescriptor );
	}
}

解决方案

It's reasonable to do it in the binder. However, I think that Decimal.Parse with the currency format provider or number style (see the docs) would be more reliable than stripping the "$" and calling base. For starters, it would handle non-US currency, which might be an issue for you some day.

这篇关于如何筛选使用自定义的模型绑定表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆