8

We know that with InputSelect we cannot use both @bind-value and @onchange...

But if we use the latter (with select instead InputSelect), how can we set a initial value different from the first ? (eg. in this sample set to 2018, the value of a variable)

Something like if (i == month) => selected

<select @onchange="ChangeYear">
    @for (int i = 2015; i < DateTime.Now.Year + 1; i++)
    {
        <option value="@i">@i</option>
    }
</select>

@code {

    int year = 2018;

    void ChangeYear(ChangeEventArgs e)
    {
        int year = Convert.ToInt32(e.Value.ToString());

        //...
    }
}
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
SandroRiz
  • 903
  • 4
  • 10
  • 18

1 Answers1

22

You can implement it in a variety of ways, as for instance, define a local variable SelectedYear that is bound to the value property of the select element, and a change event that update the SelectedYear. This actually creates a two-way data binding.

Note that SelectedYear is assigned a default value 2018

<select value="@SelectedYear" @onchange="ChangeYear">
    @for (int i = 2015; i < DateTime.Now.Year + 1; i++)
    {
        <option value="@i">@i</option>
    }
</select>

<p>@SelectedYear</p>

@code
 {
    private int SelectedYear { get; set; } = 2018;

    void ChangeYear(ChangeEventArgs e)
    {
        SelectedYear = Convert.ToInt32(e.Value.ToString());

    }
}

We know that with InputSelect we cannot use both @bind-value and @onchange

This is because the compiler add the change event handler to update the Value (Note that it is @bind-Value, not @bind-value) property of the InputSelect component to the newly selected value. This is why you could have done something like this:

<p>@SelectedYear</p>

<EditForm Model="@employees">
    <DataAnnotationsValidator />
     <div>   
        <InputSelect @bind-Value="SelectedYear" Id="SelectedYear" Class="form-control">
            @for (int i = 2015; i < DateTime.Now.Year + 1; i++)
            {
                <option value="@i">@i</option>
            }
        </InputSelect>


    </div>
    <button type="submit">Ok</button>

</EditForm>

@code
 {
    private string SelectedYear { get; set; } = "2018";

    public List<Employee> employees = new List<Employee>();


    public class Employee
    {
        public int YearBorn { get; set; }
        public string Name { get; set; }
    }
}
enet
  • 41,195
  • 5
  • 76
  • 113
  • thanks to point me to value="" attribute of select I had the doubt to do so, but I was misguided by the Intellisense of VS2019 that doesn't propose... I knew with InputSelect is easier, but sometimes in the ChangeEvent I have to do some other stuff (enabling/disabling UI pieces, reload data, etc.) – SandroRiz Dec 22 '19 at 16:21
  • 2
    For anyone else who finds this, note that when using `InputSelect`, the default that you have in the `@bind-Value` variable only works if it's the `value` of the `option` in the `Select`. It will definitively not work if you try setting it to the `text` or `display` for that `option`, and you may spend far too much time wondering why the default doesn't work! (And you don't get any errors anywhere indicating you tried to set it to a `value` that isn't value!) – Mmm Jan 07 '22 at 23:50
  • it works, but is "value" actually a valid select attribute? – symbiont Jul 01 '22 at 13:41
  • How would you pass the default selection as a parameter instead of as a string constant? – MC9000 Aug 21 '23 at 17:56