I have a Blazor component. An Id is passed in but when the OnValidSubmit of the edit form fires first it runs 'OnInitializedAsync()' as expected.
I set a parameter first to true on the page load and set that to false the first run through 'OnInitializedAsync()'.
However this value is always true so the values I want to get on only the first page load are all reset and what the user entered is ignored.
I've looked at what the scaffolder made previously (though for some reason it's not currently working) and it looks like it should work.
ChatGPT and examples online seem the same.
How can I only fire the initial get of the data on the first page load?
Thanks.
@page "/myprediction/edit/{id}"@using FootballHeadzGame.Data@using Microsoft.EntityFrameworkCore@* @inject FootballHeadz3.Data.ApplicationDbContext DB *@@inject NavigationManager NavigationManager@inject IPredictionRepository _predictionRepository@attribute [Authorize]<PageTitle>Edit Prediction</PageTitle>@if (_predictionToEdit is null){<p><em>Loading...</em></p>}else{<div class="card"><div class="card-header"><h1>Edit Prediction</h1></div><MessageComponent Message="@_message" MessageError="@_messageError" /><div class="card-body"><EditForm FormName="Prediction" Model="_predictionToEdit" OnValidSubmit="UpdatePrediction" class="row row-cols-sm-auto g-1 align-items-center"><div><DataAnnotationsValidator /><ValidationSummary /></div><div class="col-6"><label class="visually-hidden" for="inlineFormInputGroupUsername">Home Team</label><div class="input-group"><div class="input-group-text">@_predictionToEdit.HomeTeam</div><InputText @bind-Value="_predictionToEdit.HomeScorePredictionString" style="width:40px;" class="form-control"></InputText></div></div><div class="col-6"><label class="visually-hidden" for="inlineFormInputGroupUsername">Away Team</label><div class="input-group"><InputText @bind-Value="_predictionToEdit.AwayScorePredictionString" style="width:40px;" class="form-control"></InputText><div class="input-group-text">@_predictionToEdit.AwayTeam</div></div></div><div class="col-12"><button type="submit" class="btn btn-primaryfh">Update</button></div></EditForm></div></div>}@code { [Parameter] public string Id { get; set; } [CascadingParameter] Task<AuthenticationState> authenticationStateTask { get; set; } private string? _message; private string? _messageError; //private FixtureModel _predictionToEdit; [SupplyParameterFromForm] private PredictionModel _predictionToEdit { get; set; } = new(); private int userId; private bool first { get; set; } = true; // [SupplyParameterFromForm] // public MiniLeagueModel? MiniLeagueModel { get; set; } protected override async Task OnInitializedAsync() { // This is always zero not sure why //if (_predictionToEdit.UserId == 0) if (first) { var authState = await authenticationStateTask; userId = Convert.ToInt32(authState.User.Claims.FirstOrDefault().Value); _predictionToEdit.UserId = userId; _predictionToEdit.FixtureId = Convert.ToInt32(Id); // I'm not passing a prediction model in here because it passes it out and it would be a mess _predictionToEdit = await _predictionRepository.GetAsync(userId, Convert.ToInt32(Id)); _message = userId.ToString() +". " + Id; first = false; } } private async Task UpdatePrediction() { await ClearMessages(); if (_predictionToEdit.HomeScorePredictionString == null || _predictionToEdit.AwayScorePredictionString == null) { //var isNumeric = int.TryParse("123", out _); //if (int.TryParse()) _messageError = "You must fill in the score"; } else { _predictionToEdit.HomeScorePrediction = Convert.ToInt32(_predictionToEdit.HomeScorePredictionString); _predictionToEdit.AwayScorePrediction = Convert.ToInt32(_predictionToEdit.AwayScorePredictionString); await _predictionRepository.UpdateAsync(_predictionToEdit); NavigationManager.NavigateTo("/mypredictions"); } } private async Task ClearMessages() { _message = null; _messageError = null; }}