If you have used InputSelect in Blazor in ASP.NET Core 3.1 you may have
noticed bind-Value only works on a limited set of types. That is strings and
enum to be precise. In .NET 5 this will be improved. And it is easy to backport
this to 3.1.
The problem
Look on this Blazor code:
Blazor sample
This will show the user a drop list. And when the user changes value, the value
of Id will be changed. Or, at least that is what we want. Instead an exception
will be thrown with this message:
Exception
Not pretty! The reason is that InputSelect only could convert the value if the
id has the type string or some kind of enum.
The solution
This problem only exist in .NET Core 3.1. In the upcoming .NET 5 you could use
int, byte, Guid and a lot more, both nullable and not, and it just works.
Very nice! After this discovery I decided to investigate how this was solved and
tried to backport this to 3.1.
This resulted in this code:
Solution code
Then I just needed to replace InputSelect with InputSelectExtended in the
test application like this:
Blazor sample
That is all. Short a simple and will save you from boring conversion code.