From e3963158ce6d4b977f52bc6c74951c04b71575ef Mon Sep 17 00:00:00 2001 From: Geek <31265244+1257960069@users.noreply.github.com> Date: Mon, 16 Jun 2025 21:06:36 +0800 Subject: [PATCH] Fix: This handles the issue where a URL parameter like `?age=null` cannot be bound to an `int?` type Fix: This handles the issue where a URL parameter like `?age=null` cannot be bound to an `int?` type. This fix ensures that when the frontend sends a URL parameter with `null` value (e.g., `new URLSearchParams({ age: null }).toString()`), it correctly binds to a nullable integer (`int?`) instead of causing a binding error. --- .../src/ModelBinding/Binders/SimpleTypeModelBinder.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Mvc/Mvc.Core/src/ModelBinding/Binders/SimpleTypeModelBinder.cs b/src/Mvc/Mvc.Core/src/ModelBinding/Binders/SimpleTypeModelBinder.cs index 9f60e2bb7cff..a1517f0b256a 100644 --- a/src/Mvc/Mvc.Core/src/ModelBinding/Binders/SimpleTypeModelBinder.cs +++ b/src/Mvc/Mvc.Core/src/ModelBinding/Binders/SimpleTypeModelBinder.cs @@ -74,6 +74,13 @@ public Task BindModelAsync(ModelBindingContext bindingContext) // Other than the StringConverter, converters Trim() the value then throw if the result is empty. model = null; } + else if (bindingContext.ModelMetadata.IsNullableValueType && value == "null") + { + // Fix: This handles the issue where a URL parameter like `?age=null` cannot be bound to an `int?` type. + // This fix ensures that when the frontend sends a URL parameter with `null` value (e.g., `new URLSearchParams({ age: null }).toString()`), + // it correctly binds to a nullable integer (`int?`) instead of causing a binding error. + model = null; + } else { model = _typeConverter.ConvertFrom(