I have a <select>
inside my Razor View. I'm trying to save the selected option in the database whenever the selection is changed.
I'm able to do it with an <input type="text">
or any type of input, but my select doesn't work.
Here is the code :
<select name="expression@(i)"
@onchange="@(async e =>
{
// Test is always equals to "1", which is the old value of the select
var test = e.Value.ToString());
//Do Stuff
})">
@foreach (var expression in (ExpressionEnum[])Enum.GetValues(typeof(ExpressionEnum)))
{
<option value="@((int)actionItem.ExpressionEnum)" selected=@(expression == actionItem.ExpressionEnum)>
@expression.ToString()
</option>
}
</select>
As commented, the selected option of the select is actually the first one, "1".
But even if I try to select another one like the "5", the test
variable will remains equal to "1".
How can I get the correct selected value ?
actionItem.ExpressionEnum
does not seem to change during the loop which sets value
to, what I assume to be, 1 for every option in your <select>
.
I believe you should change this part:
value="@((int)actionItem.ExpressionEnum)"
to
value="@expression"