Hi
I use the SfSwitch component in a Blazor grid.
My problem is, that a property type in my models (I don't can just update the models*) is `int?`, but for something that should represent a bool e.g. `ShowInSomething`.
Now, if I add a SfSwitch component and use TChecked="int?" the read (meaning show the values from db in the grid) works, and fills out everything correct. But when I change the state (check/uncheck doesn't matter) it throws an `InvalidCastException` (Unable to cast object of type 'System.Boolean' to type 'System.Nullable`1[System.Int32]'.
at Syncfusion.Blazor.Buttons.SfSwitch`1.OnClickHandler(MouseEventArgs args)
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
).
Maybe, I am doing something wrong, but cannot see what exactly.
*) This model is scaffolded from DB, but I cannot just change the type, which would be a breaking change
Hi IT,
We have checked your reported query, and for more clarification, the Switch component TChecked property supports only the bool type. Currently we don’t have support to set the int type to the TChecked property of the switch component. For now, we can handle the switch component checked property with an int type value. Refer to the below code snippet.
|
@using Syncfusion.Blazor.Buttons
<SfSwitch Checked="@(isChecked == 1 ? true : false)" ValueChange="Change" TChecked="bool?"></SfSwitch>
@code { private int? isChecked = 1; private void Change(Syncfusion.Blazor.Buttons.ChangeEventArgs<bool?> args) { isChecked = args.Checked == true ? 1 : 0; } } |
Based on isChecked integer value (0 or 1), we can set the checked property of switch component and same time, we can update the integer value using the ValueChange event of switch component like above code snippet.
Kindly get back to us if you need any further assistance on this.
Regards,
YuvanShankar A
Ou.. I see...
A bit confusing to to me to have a `TChecked` type parameter, but the only type supported type is `bool`, doesn't it?
Anyway, this workaround doesn't help me, because (as I (implicitly) said) this switch is part of a grid cell's template.
<SfGrid>
<GridColumns>
...
<GridColumn>
<Template>
@{
var obj = context as Obj;
<SfSwitch TChecked="int?" @bind-Checked="obj.PropertyWhichIsNullableIntButRepresentsABool"></SfSwitch>
}
</Template>
</GridColumn>
<GridColumns>
</SfGrid>
Of course I can use your workaround, but have to do this for each of such property.
I ended up modifying the model (aka the column type) and hope I didn't miss an app which using it.
Thanks anyway :)
Thanks for update, IT. Please let us know if you need any further assistance on this.