Skip to content

fix: support non-string value types in MudBlazor select renderer#87

Merged
phmatray merged 1 commit intomainfrom
fix/61-generic-select-not-work
Feb 26, 2026
Merged

fix: support non-string value types in MudBlazor select renderer#87
phmatray merged 1 commit intomainfrom
fix/61-generic-select-not-work

Conversation

@phmatray
Copy link
Owner

Summary

  • Fixed MudSelect and MudSelectItem components being hardcoded to string type parameter, causing non-string select values (e.g., int, enum) to always return their default value (0 for int)
  • Made RenderSelectField and RenderSelectOptions generic on the actual model property type using reflection-based generic method dispatch
  • Added type conversion safety in UpdateFieldValue for edge cases

Changes

  • FormCraft.ForMudBlazor/Features/FormContainer/FormCraftComponent.razor.cs: Replaced RenderSelectField with a reflection-based dispatcher that calls RenderSelectFieldGeneric<TValue> using the actual property type. Made RenderSelectOptions<TValue> generic so MudSelectItem<TValue> matches the real value type. Added Convert.ChangeType fallback in UpdateFieldValue.
  • FormCraft.UnitTests/Extensions/FieldBuilderExtensionsTests.cs: Added two tests verifying that WithOptions correctly stores SelectOption<int> with preserved int value types.

Test plan

  • All 555 existing + new unit tests pass (was 553)
  • New test: WithOptions_Should_Set_Int_Options_Attribute verifies int-typed options are stored correctly
  • New test: WithOptions_Should_Preserve_Int_Value_Types verifies option values remain int, not string
  • Solution builds successfully across all target frameworks (net8.0, net9.0, net10.0)

Fixes #61

The select field renderer in FormCraftComponent hardcoded MudSelect<string>
and MudSelectItem<string>, causing all option values to be converted to
strings via ToString(). This meant non-string value types (int, enum, etc.)
always returned their default value (e.g., 0 for int) when selected.

The fix makes RenderSelectField and RenderSelectOptions generic on the
actual property type by using reflection to invoke a generic helper method
(RenderSelectFieldGeneric<TValue>) with the correct type parameter derived
from the model property. Also adds type conversion safety in UpdateFieldValue.

Fixes #61
@phmatray phmatray merged commit d4d96f2 into main Feb 26, 2026
1 check passed
@phmatray phmatray deleted the fix/61-generic-select-not-work branch February 26, 2026 23:00
@phmatray phmatray mentioned this pull request Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generic select not work

1 participant