Hi,
The following snippet shows a workaround and also makes sure that the method is async Task and not async void - the former is preferred because it lets async operations inside work before continuing to the next one, while the async void does not and can cause synchronization issues.
Repro steps:
@using System.ComponentModel.DataAnnotations
@using System.Collections.ObjectModel
@( theModel.Id == null ? "field is null" : theModel.Id.ToString() )
<EditForm Model="@theModel" OnValidSubmit="@Submit">
<DataAnnotationsValidator />
<TelerikComboBox @bind-Value="@theModel.Id" Data="@Tiers" ValueField="Id" TextField="Nom" TItem="TiersViewModel" TValue="long?" />
<TelerikButton OnClick="@ShowTiersAddView" Icon="@IconName.Plus" ButtonType="@ButtonType.Button"></TelerikButton>
<TelerikButton ButtonType="@ButtonType.Submit">SUBMIT</TelerikButton>
</EditForm>
@code {
TheValidationModel theModel { get; set; }
//private IList<TiersViewModel> Tiers { get; set; }
private ObservableCollection<TiersViewModel> Tiers { get; set; }
private async Task ShowTiersAddView() // note that the type of the method is "async Task" and not "async void"
{
Tiers.Clear();
Tiers = new ObservableCollection<TiersViewModel>(GetTiers());
await Task.Delay(20); // workaround
theModel.Id = 2;
await InvokeAsync(() => this.StateHasChanged());
}
protected override void OnInitialized()
{
theModel = new TheValidationModel();
Tiers = new ObservableCollection<TiersViewModel>(GetTiers());
}
private IList<TiersViewModel> GetTiers()
{
return Enumerable.Range(1, 15).Select(x => new TiersViewModel { Id = x, Nom = $"Nom {x}" }).ToList();
}
private void Submit()
{
Console.WriteLine("valid form submit");
}
public class TiersViewModel
{
public long? Id { get; set; }
public string Nom { get; set; }
}
public class TheValidationModel
{
[Required] public long? Id { get; set; }
}
}
Regards,
Marin Bratanov
Progress Telerik