I wanto post data to database the value of dropdown is int when i save show me below error
blazor.server.js:15 [2019-06-13T13:31:50.629Z] Error: System.InvalidOperationException: Microsoft.AspNetCore.Components.Forms.InputSelect
1[System.Int32] does not support the type 'System.Int32'.
at Microsoft.AspNetCore.Components.Forms.InputSelect1.TryParseValueFromString(String value, T& result, String& validationErrorMessage)
at Microsoft.AspNetCore.Components.Forms.InputBase
1.set_CurrentValueAsString(String value)
at Microsoft.AspNetCore.Components.Forms.InputSelect1.<BuildRenderTree>b__4_0(String __value)
at Microsoft.AspNetCore.Components.BindMethods.<>c__DisplayClass9_0.<SetValueHandler>b__0(UIEventArgs eventArgs)
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Components.Rendering.Renderer.GetErrorHandledTask(Task taskToHandle)
Thanks for reporting this issue! We think this is a duplicate of #9716.
I've investigated and determined this is not a duplicate of #9716. It's completely unrelated to that.
This is actually by design, at least currently. InputSelect
has a particular set of primitive value types it knows how to bind (see implementation here). That set only includes string
and enum types.
If we wanted, we could expand this set by default in the future. However it's also possible to extend it yourself by subclassing InputSelect
. Example:
public class InputSelectNumber<T> : InputSelect<T>
{
protected override bool TryParseValueFromString(string value, out T result, out string validationErrorMessage)
{
if (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 a valid number.";
return false;
}
}
else
{
return base.TryParseValueFromString(value, out result, out validationErrorMessage);
}
}
}
Now you'd be able to use <InputSelectNumber @bind-Value="@myIntValue">...</InputSelectNumber>
, and it would parse int values as well as register validation messages if the user chooses an unparseable value.
Sorry to comment on an old thread - but I ran into the same issue with @lpm-nova and @kardokh, fixed it with @SteveSandersonMS's solution, but after updating to preview 9 it's not working for me anymore. Should it? Any tips on how to get around it?
Thanks!
@SteveSandersonMS's code works for me on 3.0
@SteveSandersonMS this works for me
Most helpful comment
I've investigated and determined this is not a duplicate of #9716. It's completely unrelated to that.
This is actually by design, at least currently.
InputSelect
has a particular set of primitive value types it knows how to bind (see implementation here). That set only includesstring
and enum types.If we wanted, we could expand this set by default in the future. However it's also possible to extend it yourself by subclassing
InputSelect
. Example:Now you'd be able to use
<InputSelectNumber @bind-Value="@myIntValue">...</InputSelectNumber>
, and it would parse int values as well as register validation messages if the user chooses an unparseable value.