aspnetcore
aspnetcore copied to clipboard
checkbox list
Trying to create a checkbox list with the following code
@*checkbox list*@
@foreach(var item in Enum.GetValues(typeof(WebApplication1.Pages.Importance)))
{
<input type="checkbox" name="SelectedOptions" bind="SelectedOptions" value="@item.ToString()"/>@item.ToString()
}
public string SelectedOptions { get; set; }
Anything wrong with this approach? I get the following error, when I click on the checkbox
Uncaught Error: System.InvalidCastException: Specified cast is not valid.
at Microsoft.AspNetCore.Blazor.Components.BindMethods+<>c__DisplayClass5_0.<SetValueHandler>b__0 (:54167/Microsoft.AspNetCore.Blazor.UIEventArgs _) <0x1e34a40 + 0x00020> in <87781d5cdc00468a86a111ddf9fe3127>:0
at Microsoft.AspNetCore.Blazor.Components.BlazorComponent.Microsoft.AspNetCore.Blazor.Components.IHandleEvent.HandleEvent (:54167/Microsoft.AspNetCore.Blazor.UIEventHandler handler, Microsoft.AspNetCore.Blazor.UIEventArgs args) <0x1e347d8 + 0x00018> in <87781d5cdc00468a86a111ddf9fe3127>:0
at Microsoft.AspNetCore.Blazor.Rendering.ComponentState.DispatchEvent (:54167/Microsoft.AspNetCore.Blazor.UIEventHandler handler, Microsoft.AspNetCore.Blazor.UIEventArgs eventArgs) <0x1e3ce38 + 0x00034> in <87781d5cdc00468a86a111ddf9fe3127>:0
at Microsoft.AspNetCore.Blazor.Rendering.Renderer.DispatchEvent (:54167/System.Int32 componentId, System.Int32 eventHandlerId, Microsoft.AspNetCore.Blazor.UIEventArgs eventArgs) <0x1e3ca20 + 0x00050> in <87781d5cdc00468a86a111ddf9fe3127>:0
at Microsoft.AspNetCore.Blazor.Browser.Rendering.BrowserRenderer.DispatchBrowserEvent (:54167/System.Int32 componentId, System.Int32 eventHandlerId, Microsoft.AspNetCore.Blazor.UIEventArgs eventArgs) <0x1e3c8b8 + 0x00020> in <a63605fd920f4984bd96e24578831e55>:0
at Microsoft.AspNetCore.Blazor.Browser.Rendering.BrowserRendererEventDispatcher.DispatchEvent (:54167/System.String eventDescriptorJson, System.String eventArgsJson) <0x1dfb870 + 0x0005a> in <a63605fd920f4984bd96e24578831e55>:0
at Microsoft.AspNetCore.Blazor.Components.BindMethods+<>c__DisplayClass5_0.<SetValueHandler>b__0 (:54167/Microsoft.AspNetCore.Blazor.UIEventArgs _) <0x1e34a40 + 0x00020> in <87781d5cdc00468a86a111ddf9fe3127>:0
at Microsoft.AspNetCore.Blazor.Components.BlazorComponent.Microsoft.AspNetCore.Blazor.Components.IHandleEvent.HandleEvent (:54167/Microsoft.AspNetCore.Blazor.UIEventHandler handler, Microsoft.AspNetCore.Blazor.UIEventArgs args) <0x1e347d8 + 0x00018> in <87781d5cdc00468a86a111ddf9fe3127>:0
at Microsoft.AspNetCore.Blazor.Rendering.ComponentState.DispatchEvent (:54167/Microsoft.AspNetCore.Blazor.UIEventHandler handler, Microsoft.AspNetCore.Blazor.UIEventArgs eventArgs) <0x1e3ce38 + 0x00034> in <87781d5cdc00468a86a111ddf9fe3127>:0
at Microsoft.AspNetCore.Blazor.Rendering.Renderer.DispatchEvent (:54167/System.Int32 componentId, System.Int32 eventHandlerId, Microsoft.AspNetCore.Blazor.UIEventArgs eventArgs) <0x1e3ca20 + 0x00050> in <87781d5cdc00468a86a111ddf9fe3127>:0
at Microsoft.AspNetCore.Blazor.Browser.Rendering.BrowserRenderer.DispatchBrowserEvent (:54167/System.Int32 componentId, System.Int32 eventHandlerId, Microsoft.AspNetCore.Blazor.UIEventArgs eventArgs) <0x1e3c8b8 + 0x00020> in <a63605fd920f4984bd96e24578831e55>:0
at Microsoft.AspNetCore.Blazor.Browser.Rendering.BrowserRendererEventDispatcher.DispatchEvent (:54167/System.String eventDescriptorJson, System.String eventArgsJson) <0x1dfb870 + 0x0005a> in <a63605fd920f4984bd96e24578831e55>:0
at Object.callMethod (MonoPlatform.ts:70)
at raiseEvent (BrowserRenderer.ts:304)
at EventDelegator.onEvent (BrowserRenderer.ts:17)
at EventDelegator.onGlobalEvent (EventDelegator.ts:77)
Checkboxes produce bool values, so that's the type you should be binding it to, not string.
BTW I appreciate the error could be clearer about this.