Skip to content

Instantly share code, notes, and snippets.

@weslley39
Created June 27, 2014 18:11
Show Gist options
  • Save weslley39/ad746cd73e7fb8771808 to your computer and use it in GitHub Desktop.
Save weslley39/ad746cd73e7fb8771808 to your computer and use it in GitHub Desktop.
Work W/ Decimal ASP 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