You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
This happen when using model binding in Asp.Net MVC 6
If you have a datetime property (e.g. Birthday) and you bind it to an input element. when you enter an invalid value for the date time (e.g. abc), the binding process adds a model state error with the message
The parameter conversion from type 'System.String' to type 'System.DateTime' failed. See the inner exception for more information
This message gets displayed to the user as well (using the HtmlHelper ValidationMessageFor)
The error is thrown in class ValueProviderResult, function ConvertSimpleType - where a FormatException is wrapped and rethrown as a InvalidOperationException. This gets added to the ModelState error in the class TypeConverterModelBinder.
In MVC 4, the same Exception is thrown by ValueProviderResult but it is processed down in the chain and changed to
The value 'abc' is not valid for Birthday
This happens in class DefaultModelBinder.cs , function BindProperty. The code to transform the errors is:
// Convert FormatExceptions (type conversion failures) into InvalidValue messagesforeach(ModelError error in modelState.Errors.Where(err => String.IsNullOrEmpty(err.ErrorMessage)&& err.Exception !=null).ToList()){for(Exceptionexception= error.Exception;exception!=null;exception= exception.InnerException){// We only consider "known" type of exception and do not make too aggressive changes hereif(exception isFormatException|| exception is OverflowException){stringdisplayName= propertyMetadata.GetDisplayName();stringerrorMessageTemplate= GetValueInvalidResource(controllerContext);stringerrorMessage= String.Format(CultureInfo.CurrentCulture, errorMessageTemplate, modelState.Value.AttemptedValue, displayName);
modelState.Errors.Remove(error);
modelState.Errors.Add(errorMessage);break;}}}
Something similar should also be added for vNext - to make it more user friendly.
The text was updated successfully, but these errors were encountered:
This happen when using model binding in Asp.Net MVC 6
If you have a datetime property (e.g. Birthday) and you bind it to an input element. when you enter an invalid value for the date time (e.g. abc), the binding process adds a model state error with the message
This message gets displayed to the user as well (using the HtmlHelper ValidationMessageFor)
The error is thrown in class ValueProviderResult, function ConvertSimpleType - where a FormatException is wrapped and rethrown as a InvalidOperationException. This gets added to the ModelState error in the class TypeConverterModelBinder.
In MVC 4, the same Exception is thrown by ValueProviderResult but it is processed down in the chain and changed to
This happens in class DefaultModelBinder.cs , function BindProperty. The code to transform the errors is:
Something similar should also be added for vNext - to make it more user friendly.
The text was updated successfully, but these errors were encountered: