dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.37k stars 9.99k forks source link

Input validation status gets mixed up when toggling content #37391

Closed carlbjorknas closed 3 years ago

carlbjorknas commented 3 years ago

Describe the bug

When toggling visibility of inputs, the input can get the wrong validation status color (due to input component reuse?).

Toggling in blazor

To Reproduce

Code nuget: microsoft.aspnetcore.components.dataannotations.validation\3.2.0-rc1.20223.4 ``` @page "/" @using System.ComponentModel.DataAnnotations;

Input toggle test

@{ var index = 1; } @foreach (var model in _model.Models) { @if (model.Visible) {
Modelindex: @(index)
} index++; }
@code{ private EditContext _editContext; MainModel _model = new MainModel(); protected override void OnInitialized() { base.OnInitialized(); _editContext = new EditContext(_model); } class MainModel { [ValidateComplexType] public List Models { get; } = new List { new SubModel {Visible = true, TheValue = 1}, new SubModel() {TheValue = 6} }; } class SubModel : IValidatableObject { public int TheValue { get; set; } public bool Visible { get; set; } public IEnumerable Validate(ValidationContext validationContext) { if (TheValue < 5) yield return new ValidationResult("Must be 5 or greater.", new[] { nameof(TheValue) }); } } } ```

Further technical details

dotnet --info Output ``` .NET SDK (reflecting any global.json): Version: 5.0.401 Commit: 4bef5f3dbf Runtime Environment: OS Name: Windows OS Version: 10.0.19043 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\5.0.401\ Host (useful for support): Version: 5.0.10 Commit: e1825b4928 .NET SDKs installed: 5.0.302 [C:\Program Files\dotnet\sdk] 5.0.401 [C:\Program Files\dotnet\sdk] .NET runtimes installed: Microsoft.AspNetCore.All 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.19 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.19 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] ```
mkArtakMSFT commented 3 years ago

Thanks for contacting us. It looks like you're missing the @key attribute for the elements which are rendered in a loop. You can learn more about this from https://docs.microsoft.com/en-us/aspnet/core/blazor/components/?view=aspnetcore-6.0#use-key-to-control-the-preservation-of-elements-and-components