Skip to content

New TryParseModelBinder#40233

Merged
brunolins16 merged 34 commits into
dotnet:mainfrom
brunolins16:brunolins16/issues/39682
Mar 3, 2022
Merged

New TryParseModelBinder#40233
brunolins16 merged 34 commits into
dotnet:mainfrom
brunolins16:brunolins16/issues/39682

Conversation

@brunolins16
Copy link
Copy Markdown
Member

@brunolins16 brunolins16 commented Feb 15, 2022

Fixes #39682

Comment thread src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs
Comment thread src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs
@Pilchie Pilchie added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Feb 15, 2022
Comment thread src/Mvc/Mvc.Abstractions/src/ModelBinding/ModelMetadata.cs Outdated
Comment thread src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs Outdated
Comment thread src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs Outdated
Comment thread src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs
Comment thread src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs Outdated
Comment thread src/Mvc/Mvc.Abstractions/src/ModelBinding/ModelMetadata.cs Outdated
Comment thread src/Http/Http.Extensions/src/RequestDelegateFactory.cs
@brunolins16 brunolins16 marked this pull request as ready for review February 17, 2022 22:27
@brunolins16 brunolins16 requested a review from javiercn February 17, 2022 22:28
@brunolins16 brunolins16 marked this pull request as draft February 17, 2022 22:32
@brunolins16 brunolins16 marked this pull request as ready for review February 18, 2022 16:16
Comment thread src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs Outdated
Comment thread src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinderProvider.cs Outdated
Comment thread src/Mvc/Mvc.Core/src/ModelBinding/Metadata/DefaultBindingMetadataProvider.cs Outdated
Comment thread src/Mvc/Mvc.Abstractions/src/ModelBinding/ModelMetadata.cs Outdated
Comment thread src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs
Comment thread src/Mvc/Mvc.Core/src/MvcOptions.cs Outdated
Comment thread src/Mvc/Mvc.Core/src/Resources.resx Outdated
Comment thread src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs
Comment thread src/Mvc/Mvc.Core/test/ModelBinding/Metadata/DefaultModelMetadataTest.cs Outdated
Comment thread src/Mvc/Mvc.Core/test/ModelBinding/Metadata/DefaultModelMetadataTest.cs Outdated
Comment thread src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs Outdated
Copy link
Copy Markdown
Contributor

@pranavkm pranavkm left a 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
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Member Author

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?

Comment thread src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs Outdated
Comment thread src/Mvc/Mvc.Core/src/ModelBinding/Binders/TryParseModelBinder.cs Outdated
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()))),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

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)

Comment thread src/Mvc/Mvc.Core/src/ModelBinding/Metadata/DefaultBindingMetadataProvider.cs Outdated
@brunolins16 brunolins16 enabled auto-merge (squash) March 3, 2022 18:24
@brunolins16 brunolins16 merged commit 3817558 into dotnet:main Mar 3, 2022
@ghost ghost added this to the 7.0-preview3 milestone Mar 3, 2022
@brunolins16 brunolins16 deleted the brunolins16/issues/39682 branch August 2, 2022 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider adding support for TryParse as a way to bind primitives

5 participants