Skip to content

Commit e396315

Browse files
authored
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.
1 parent 9e08e07 commit e396315

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/Mvc/Mvc.Core/src/ModelBinding/Binders/SimpleTypeModelBinder.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
7474
// Other than the StringConverter, converters Trim() the value then throw if the result is empty.
7575
model = null;
7676
}
77+
else if (bindingContext.ModelMetadata.IsNullableValueType && value == "null")
78+
{
79+
// Fix: This handles the issue where a URL parameter like `?age=null` cannot be bound to an `int?` type.
80+
// This fix ensures that when the frontend sends a URL parameter with `null` value (e.g., `new URLSearchParams({ age: null }).toString()`),
81+
// it correctly binds to a nullable integer (`int?`) instead of causing a binding error.
82+
model = null;
83+
}
7784
else
7885
{
7986
model = _typeConverter.ConvertFrom(

0 commit comments

Comments
 (0)