I'm coding a Blazor Server Web Application, specifically a form. I'm struggling with an InputSelect field, and I couldn't find any solution to this specific problem.
Here's a basic example I made based on my actual code:
@page "/problem-sample"<EditForm Model="@formModel" OnSubmit="HandleFormSubmit"><InputFile OnChange="HandleFileUpload" /></EditForm><h3>@(fileIsComplete ? "A file is uploaded" : "A file isn't uploaded")</h3>@code { public class FormModel { public IBrowserFile? File { get; set; } } private FormModel formModel = new(); private bool fileIsComplete = false; public async Task HandleFormSubmit() { if (formModel.File == null) { return; } } private async Task HandleFileUpload(InputFileChangeEventArgs e) { var file = e.File; if (file != null) { fileIsComplete = true; return; } }}The expected behaviour is:
- If the user doesn't upload any file,
fileIsCompleteshould be false. - If the user hits the button and uploads a file,
fileIsCompleteshould be true. - If the user hits the button, but presses cancel or closes the window,
fileIsCompleteshould be false, even if the user previously uploaded a file.
Currently, if the user cancels or closes the window after uploading a file, InputFile clears, but fileIsComplete keeps true:
I tried checking if the file is nullable, using expression body or using stateHasChanged directly, but nothing seems to work.
