aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Blazor - Select drpdown list issue

Open AnkitSharma-007 opened this issue 8 years ago • 3 comments

code 1:

<div>
                <label for="Country" class="control-label">Country</label>
                <select  class="form-control" bind="@id" onchange="@click">
                    <option value="">-- Select Country --</option>
                    @@foreach (var country in countryList)
                    {
                        <option value="@country.CountryId">@country.CountryName</option>
                    }
                </select>
            </div>
 <p>Country: @id</p>

Code 2:

<div>
                <label for="Country" class="control-label">Country</label>
                <select class="form-control" onchange="@click" bind="@id">
                    <option value="">-- Select Country --</option>
                    @foreach (var country in countryList)
                    {
                        <option value="@country.CountryId">@country.CountryName</option>
                    }
                </select>
            </div>
 <p>Country: @id</p>

Function Section:

@functions {
Country[] countryList;
public string id { get; set; }

protected override async Task OnInitAsync()
{
    countryList = await Http.GetJsonAsync<Country[]>("Countries/GetCountryList");
}

void click()
{
    Console.WriteLine("Hello");

}
}

Please note the order of writing bind and onchange event in select tag in Code 1 and Code 2.

Wahen i am using the Code 1 , The value of id is not getting bind but onchange event is firing. When i am using Code 2, the value of id is getting bind but onchange event is not firing.

Can anyone help me with this. I want to achieve both i.e. bind the value of id as well as fire onchange event on selecting a value in dropdown list.

AnkitSharma-007 avatar May 10 '18 18:05 AnkitSharma-007

Both are not allowed aspnet/Blazor#524 you could do

<select class="form-control"  bind="@Id" >
                    <option value="">-- Select Country --</option>
                    @foreach (var country in countryList)
                    {
                    <option value="@country.CountryId">@country.CountryName</option>
                    }
                </select>
 private string _id;

        public string Id
        {
            get { return _id; }
            set { _id = value; Console.WriteLine(value); }
        }

dlr1 avatar May 10 '18 18:05 dlr1

Thanks for your Answer. aspnet/Blazor#524 Solves my problem.

I have one additional question here. Why it is behaving differently in both cases. It is always firing the event which is in the last. In case 1, onchange is invoked but in case 2 bind is invoked.

AnkitSharma-007 avatar May 10 '18 19:05 AnkitSharma-007

@AnkitSharma-007 It's purely a side-effect of the limitation that there's only one onchange handler. Whichever one goes last is overwriting the first.

SteveSandersonMS avatar May 11 '18 21:05 SteveSandersonMS