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