I'm working on a Blazor Wasm. I would like to create my own custom select
component. The implementation below works but something is missing: there is no callback when something is selected inside my custom select component. I Would like to be able to catch the event from my TestView.razor component.
My custom implementation InputSelectCustom.cs
public class InputSelectCustom<T> : InputSelect<T>
{
protected override string FormatValueAsString(T value)
{
// Custom code ommitted for clarity
return base.FormatValueAsString(value);
}
protected override bool TryParseValueFromString(string value, out T result, out string validationErrorMessage)
{
if (typeof(T) == typeof(int) ||
typeof(T) == typeof(int?))
{
if (int.TryParse(value, out var resultInt))
{
result = (T)(object)resultInt;
validationErrorMessage = null;
return true;
}
else
{
result = default;
validationErrorMessage = "The chosen value is not valid.";
return false;
}
}
else
if (typeof(T).IsEnum)
{
if (CustomFunctions.EnumTryParse<T>(value, out var resultEnum))
{
result = (T)(object)resultEnum;
validationErrorMessage = null;
return true;
}
else
{
result = default;
validationErrorMessage = "The chosen value is not valid.";
return false;
}
}
else
if (typeof(T).IsNullableEnum())
{
if (CustomFunctions.NullableEnumTryParse<T>(value, out var resultEnum))
{
result = (T)(object)resultEnum;
validationErrorMessage = null;
return true;
}
else
{
result = default;
validationErrorMessage = "The chosen value is not valid.";
return false;
}
}
else
{
return base.TryParseValueFromString(value, out result, out validationErrorMessage);
}
}
protected override Task OnParametersSetAsync()
{
return base.OnParametersSetAsync();
}
}
My TestView.razor
@page "/test-view"
<EditForm Model="CurrentFilterModel">
<InputSelectCustom @bind-Value="@CurrentFilterModel.ClassmentFilter">
<option value="null"> Show all content </option>
<option value="@EnumClassment.Popular"> Popular </option>
<option value="@EnumClassment.Featured"> Featured </option>
<option value="@EnumClassment.Novelty"> Novelty </option>
</InputSelectCustom>
</EditForm>
@code
{
public FilterModel CurrentFilterModel = new FilterModel();
public class FilterModel
{
public EnumClassment? ClassmentFilter { get; set; }
}
}
My enum :
public enum EnumClassment
{
Featured,
Popular,
Novelty
}
UPDATE
So what I needed was a way to be notified whenever my selected value changed.
At first, I tried with this:
<InputSelectCustom @bind-Value="CurrentFilterModel.ClassmentFilter">
<option value=""> Show all content </option>
<option value="@EnumClassment.Popular"> Popular </option>
<option value="@EnumClassment.Featured"> Featured </option>
<option value="@EnumClassment.Novelty"> Novelty </option>
</InputSelectCustom>
Selected value changed correctly but with the code above I have no way to be notified of value change. Per info, I need to be notified because whenever user change selection, I would like to immediately perform server side calls for filtering data.
So below is my second attempt:
<InputSelectCustom @bind-Value="CurrentFilterModel.ClassmentFilter" ValueChanged="ValueChangedForClassmentFilter">
<option value=""> Show all content </option>
<option value="@EnumClassment.Popular"> Popular </option>
<option value="@EnumClassment.Featured"> Featured </option>
<option value="@EnumClassment.Novelty"> Novelty </option>
</InputSelectCustom>
As you can see I cannot use @bind-Value
and ValueChanged
at the same time.
So here comes my final solution:
<InputSelectCustom Value="@CurrentFilterModel.ClassmentFilter" ValueExpression="@( () => CurrentFilterModel.ClassmentFilter )" ValueChanged="@( (EnumClassment? s) => ValueChangedForClassmentFilter(s) )">
<option value=""> Show all content </option>
<option value="@EnumClassment.Popular"> Popular </option>
<option value="@EnumClassment.Featured"> Featured </option>
<option value="@EnumClassment.Novelty"> Novelty </option>
</InputSelectCustom>
protected async Task ValueChangedForClassmentFilter(EnumClassment? theUserInput)
{
// You have to update the model manually because handling the ValueChanged event does not let you use @bind-Value
// For the validation to work you must now also define the ValueExpression because @bind-Value did it for you
CurrentFilterModel.ClassmentFilter = theUserInput;
// Refresh data based on filters
await FilterCoursesServerSide();
}
This time, everything is working as expected: whenever user select another value, I am notified of the new value and I can immediately execute custom code.
Please correct me if I'm wrong or if this can be done a more elegant way because I am new to Blazor and maybe I missed something.
PS: Tested with strings, integers and enumerations.