Created
June 27, 2014 18:14
-
-
Save kidchenko/3a9161afc1e7121a579d to your computer and use it in GitHub Desktop.
Make decimal work in asp net mvc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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