Skip to content

Instantly share code, notes, and snippets.

@kidchenko
Created June 27, 2014 18:14
Show Gist options
  • Save kidchenko/3a9161afc1e7121a579d to your computer and use it in GitHub Desktop.
Save kidchenko/3a9161afc1e7121a579d to your computer and use it in GitHub Desktop.
Make decimal work in asp net mvc
public class DecimalModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext,
ModelBindingContext bindingContext)
{
ValueProviderResult valueResult = bindingContext.ValueProvider
.GetValue(bindingContext.ModelName);
ModelState modelState = new ModelState { Value = valueResult };
object actualValue = null;
try
{
actualValue = Convert.ToDecimal(valueResult.AttemptedValue,
CultureInfo.CurrentCulture);
}
catch (FormatException e)
{
modelState.Errors.Add(e);
}
bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
return actualValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment