M K
M K

Reputation: 2157

EditContext.IsModified still false after unchecking checkbox

I have some checkboxes on an EditForm and I'm getting inconsistent behavior with EditContext.IsModified(). When you click open button, uncheck the first checkbox and click "OK", you see that IsModified() still returned false. Please refer below sample code.

<button @onclick="OnOpen">Open</button>


@if (_weatherForecast != null)
{
    @if (_wasModified.HasValue)
    {
        <p>EditContext.IsModified() = @_wasModified</p>
        <p>WillRain = @_weatherForecast.WillRain</p>
        <p>WillSnow = @_weatherForecast.WillSnow</p>
    }
    @if (_isVisible)
    { 
                <EditForm Model="@_weatherForecast" OnValidSubmit="OnValidSubmit">
                    <DataAnnotationsValidator />
                    <div class="form-row">
                        <div class="form-group col">
                            <label>Will rain </label>
                            <input type="checkbox" @bind="@_weatherForecast.WillRain" />
                        </div>
                    </div>
                    <div class="form-row">
                        <div class="form-group col">
                            <label>Will snow </label>
                            <input type="checkbox" @bind="@_weatherForecast.WillSnow" />
                        </div>
                    </div>
                    <div class="e-footer-content">
                        <button> Ok</button>
                    </div>
                </EditForm>
    }

}
@code {
    private bool _isVisible = false;
    private WeatherForecast _weatherForecast;
    private bool? _wasModified;

    private void OnValidSubmit(EditContext editContext)
    {
        _wasModified = editContext.IsModified();
        _isVisible = false;
    }

    private void OnOpen()
    {
        _weatherForecast = new WeatherForecast { WillRain = true, WillSnow = false };
        _wasModified = null;
        _isVisible = true;
    }

    public class WeatherForecast
    {
        public bool WillRain { get; set; }
        public bool WillSnow { get; set; }
    }
}

Upvotes: 2

Views: 1300

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273244

When you want EditForm support you will have to use <InputCheckbox /> instead of <input type="checkbox" />

In your case

<InputCheckbox @bind-Value="_weatherForecast.WillRain" />

Upvotes: 2

Related Questions