Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Answer was awarded to Freddy Rios since his link provided me with the base to do this, but the code needed some fixing up:</p> <pre><code> // http://www.crydust.be/blog/2009/07/30/custom-model-binder-to-avoid-decimal-separator-problems/ public class MoneyParsableModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { object result = null; // Added support for decimals and nullable types - c. if ( bindingContext.ModelType == typeof(double) || bindingContext.ModelType == typeof(decimal) || bindingContext.ModelType == typeof(double?) || bindingContext.ModelType == typeof(decimal?) ) { string modelName = bindingContext.ModelName; string attemptedValue = bindingContext.ValueProvider[modelName].AttemptedValue; // Depending on cultureinfo the NumberDecimalSeparator can be "," or "." // Both "." and "," should be accepted, but aren't. string wantedSeperator = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator; string alternateSeperator = (wantedSeperator == "," ? "." : ","); if (attemptedValue.IndexOf(wantedSeperator) == -1 &amp;&amp; attemptedValue.IndexOf(alternateSeperator) != -1) { attemptedValue = attemptedValue.Replace(alternateSeperator, wantedSeperator); } // If SetModelValue is not called it may result in a null-ref exception if the model is resused - c. bindingContext.ModelState.SetModelValue(modelName, bindingContext.ValueProvider[modelName]); try { // Added support for decimals and nullable types - c. if (bindingContext.ModelType == typeof(double) || bindingContext.ModelType == typeof(double?)) { result = double.Parse(attemptedValue, NumberStyles.Any); } else { result = decimal.Parse(attemptedValue, NumberStyles.Any); } } catch (FormatException e) { bindingContext.ModelState.AddModelError(modelName, e); } } else { result = base.BindModel(controllerContext, bindingContext); } return result; } } </code></pre> <p>It ain't pretty, but it works.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload