Skip to content

Cannot cast string to Boolean #34816

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

Closed
PersyJack opened this issue Jul 28, 2021 · 0 comments · Fixed by #35318
Closed

Cannot cast string to Boolean #34816

PersyJack opened this issue Jul 28, 2021 · 0 comments · Fixed by #35318
Assignees
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. Done This issue has been fixed
Milestone

Comments

@PersyJack
Copy link

In Blazor, having an input group with Boolean type, throws an exception. This is the setup:
<InputRadio TValue="bool" Value="true" />

generates : <input type="radio" name="78ab199033a74bc0be851de4fcf8e64a" value="True">

Following exception is thrown:

Error: System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Boolean'. at Microsoft.AspNetCore.Components.BindConverter.ConvertToBoolCore(Object obj, CultureInfo culture, Boolean& value) in Microsoft.AspNetCore.Components.dll:token 0x600003a+0x0 at Microsoft.AspNetCore.Components.BindConverter.TryConvertTo[T](Object obj, CultureInfo culture, T& value) in Microsoft.AspNetCore.Components.dll:token 0x6000066+0x0 at Microsoft.AspNetCore.Components.Forms.InputExtensions.TryParseSelectableValueFromString[TValue](InputBase1 input, String value, TValue& result, String& validationErrorMessage) in Microsoft.AspNetCore.Components.Web.dll:token 0x6000171+0x0
at Microsoft.AspNetCore.Components.Forms.InputRadioGroup1.TryParseValueFromString(String value, TValue& result, String& validationErrorMessage) in Microsoft.AspNetCore.Components.Web.dll:token 0x60001e8+0x0 at Microsoft.AspNetCore.Components.Forms.InputBase1.set_CurrentValueAsString(String value) in Microsoft.AspNetCore.Components.Web.dll:token 0x6000152+0x3a
at Microsoft.AspNetCore.Components.Forms.InputRadioGroup1.<OnParametersSet>b__14_0(String __value) in Microsoft.AspNetCore.Components.Web.dll:token 0x60001ea+0x0 at Microsoft.AspNetCore.Components.EventCallbackFactoryBinderExtensions.<>c__DisplayClass24_01.b__0(ChangeEventArgs e) in Microsoft.AspNetCore.Components.dll:token 0x600037a+0x2c
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task) in Microsoft.AspNetCore.Components.dll:token 0x60000a2+0x65
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState) in Microsoft.AspNetCore.Components.dll:token 0x600023e+0x65`

Related code expects a boolean already not a string so it crashes:

private static bool ConvertToBoolCore(object? obj, CultureInfo? culture, out bool value)
{
// We expect the input to already be a bool.
value = (bool)obj!;
return true;
}

@mkArtakMSFT mkArtakMSFT added area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. labels Jul 28, 2021
@mkArtakMSFT mkArtakMSFT added this to the 6.0-rc2 milestone Jul 29, 2021
@ghost ghost added Done This issue has been fixed and removed Working labels Aug 19, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Sep 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. Done This issue has been fixed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants