using System; using System.Web.Mvc; namespace Neo.Legitimate.Web.FrameworkExtensions { /// /// Custom decimal binder. /// Add the following to Global.Asax /// ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder()); /// ModelBinders.Binders.Add(typeof(decimal?), new DecimalModelBinder()); /// public class DecimalModelBinder : DefaultModelBinder { #region Implementation of IModelBinder public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); if(valueProviderResult.AttemptedValue.Equals("N.aN") || valueProviderResult.AttemptedValue.Equals("NaN") || valueProviderResult.AttemptedValue.Equals("Infini.ty") || valueProviderResult.AttemptedValue.Equals("Infinity") || string.IsNullOrEmpty(valueProviderResult.AttemptedValue)) return 0m; return valueProviderResult == null ? base.BindModel(controllerContext, bindingContext) : Convert.ToDecimal(valueProviderResult.AttemptedValue); } #endregion } }