-
Notifications
You must be signed in to change notification settings - Fork 10.5k
New TryParseModelBinder #40233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New TryParseModelBinder #40233
Conversation
src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinderProvider.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/src/ModelBinding/Metadata/DefaultBindingMetadataProvider.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/test/ModelBinding/Metadata/DefaultModelMetadataTest.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Brennan <[email protected]>
src/Mvc/Mvc.Core/test/ModelBinding/Metadata/DefaultModelMetadataTest.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs
Outdated
Show resolved
Hide resolved
pranavkm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for driving this PR!
| } | ||
| catch (InvalidOperationException) | ||
| { | ||
| // The ParameterBindingMethodCache.FindTryParseMethod throws an exception |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion - we could update FindTryParseMethod to not throw via an additional parameter. Not catching exceptions is certainly a lot nicer since it avoids things like first-chance exceptions in VS.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. I will open a new PR just for that, ok?
src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs
Outdated
Show resolved
Hide resolved
| Expression.Block( | ||
| Expression.Assign(modelValue, Expression.Convert(parsedValue, modelValue.Type)), | ||
| Expression.Assign(BindingResultExpression, Expression.Call(SuccessBindingResultMethod, modelValue))), | ||
| Expression.Call(AddModelErrorMethod, BindingContextExpression, Expression.Constant(new FormatException()))), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is exactly what the TryAddModelError will do:
| if (exception is FormatException || exception is OverflowException) |
src/Mvc/Mvc.Core/src/ModelBinding/Metadata/DefaultBindingMetadataProvider.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Pranav K <[email protected]>
Fixes #39682