Completed
Last Updated: 15 Apr 2020 13:42 by ADMIN
Release 2.11.0
Sylvain
Created on: 07 Apr 2020 11:48
Category: ComboBox
Type: Bug Report
1
Value and Validation state do not update after changing the data and the Value manually
When the Data and the Value of the combo box change, the combo does not let the model update its value.
1 comment
ADMIN
Marin Bratanov
Posted on: 07 Apr 2020 11:52

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:

  1. click Submit - form is invalid
  2. click the Plus icon - value is set (without the workaround it isn't which is the problem). THe invalid state remains, and it also remains with the standard form inputs which indicates that this may be a framework behavior. Perhaps using an EditContext and calling its .Validate() method would update the validation.
  3. click Submit - expected behavior is that the form submits.

 

@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

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.