Skip to content

Cannot cast string to Boolean #34816

Closed
@PersyJack

Description

@PersyJack

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;
}

Metadata

Metadata

Assignees

Labels

DoneThis issue has been fixedarea-blazorIncludes: Blazor, Razor ComponentsbugThis issue describes a behavior which is not expected - a bug.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions