-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Closed
Closed
Copy link
Labels
area-blazorIncludes: Blazor, Razor ComponentsIncludes: Blazor, Razor Componentsarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-blazor-form-validationThis issue is related to forms validation in BlazorThis issue is related to forms validation in Blazor
Milestone
Description
Is there an existing issue for this?
- I have searched the existing issues
Is your feature request related to a problem? Please describe the problem.
From a Minimal API project
With the HTML form
<form method="post" action="/SomeUrl">
<input type="text" name="someProp" />
<button type="submit">OK</button>
</form>
And Mapping
app.MapPost("/SomeUrl", ([FromForm] SomeModel theInputModel) => Results.Ok());
And Model
public class SomeModel
{
public int? SomeProp { get; set; }
}
If I enter a number in "SomeProp" input field, it works ok.
But if I leave "SomeProp" input field empty and the submit the form, I get the exception:
Microsoft.AspNetCore.Http.BadHttpRequestException: The value '' is not valid for 'SomeProp'.
---> Microsoft.AspNetCore.Components.Endpoints.FormMapping.FormDataMappingException: An error occurred while trying to map a value from form data. For more details, see the 'Error' property and the 'InnerException' property.
at Microsoft.AspNetCore.Components.Endpoints.FormMapping.FormDataReader.AddMappingError(Exception exception, String attemptedValue)
at Microsoft.AspNetCore.Components.Endpoints.FormMapping.CompiledComplexTypeConverter`1.TryRead(FormDataReader& context, Type type, FormDataMapperOptions options, T& result, Boolean& found)
at Microsoft.AspNetCore.Components.Endpoints.FormMapping.FormDataMapper.Map[T](FormDataReader reader, FormDataMapperOptions options)
at lambda_method280(Closure, Object, HttpContext, Object)
--- End of inner exception stack trace ---
at Microsoft.AspNetCore.Http.RequestDelegateFactory.Log.FormDataMappingFailed(HttpContext httpContext, String parameterTypeName, String parameterName, FormDataMappingException exception, Boolean shouldThrow)
at lambda_method280(Closure, Object, HttpContext, Object)
at Microsoft.AspNetCore.Http.RequestDelegateFactory.<>c__DisplayClass104_2.<<HandleRequestBodyAndCompileRequestDelegateForForm>b__2>d.MoveNext()
--- End of stack trace from previous location ---
That is because the request sent to the server when posting the form is a POST with content :
someProp=
The only workaroung I found is using a string instead of int? and then convert string to int? by myself which is not the best solution.
Describe the solution you'd like
When binding [FromForm], I beleive that Minimal API should convert empty string to null when converting to a Nullable type (ex int?).
Or at least give any option to do so.
Additional context
No response
KennethHoff, flatproject, dcernach, madeyoga, alexkuznetsov and 6 morealexkuznetsov
Metadata
Metadata
Assignees
Labels
area-blazorIncludes: Blazor, Razor ComponentsIncludes: Blazor, Razor Componentsarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-blazor-form-validationThis issue is related to forms validation in BlazorThis issue is related to forms validation in Blazor