I have a simple page in my .NET 8 Blazor Client project (see code below).
The code was always giving me “FirstName is required” and “LastName is required” messages. I didn’t understand why – it was structured similar to: https://learn.microsoft.com/en-us/aspnet/core/blazor/forms/validation?view=aspnetcore-8.0
I decided to see if it would work server side, to see if that was a constraint. I pasted the code file into the Server project in my solution then added rendermode InteractiveServer
.
It worked.
I then moved this file back to the Client project, changed render mode to InteractiveAuto
, did a Clean and Rebuild all, which I expected (hoped!) would clean any cached WebAssembly. To my surprise it now worked on the client.
There was absolutely no fancy code communicating with the server. What you see is “what is”. Why has it started working?
Now, I don’t want to accept this code as working, for all I know the interactive server version that first worked could be cached somehow.
Can anyone tell me if this is valid .NET 8 Blazor client code and, if not, what I need to add or change to it?
In addition, is there a way to check that the WebAssembly loaded is completely fresh (ie: not cached by Edge browser)
Thanks.
@page "/formdataentryclient"
@using System.ComponentModel.DataAnnotations
@rendermode InteractiveAuto
<h3>Data Entry Form using EditForm and built in Input components</h3>
<EditForm Model="@person" FormName="DataEntryForm" Enhance OnInvalidSubmit="OnInvalidSubmit" OnValidSubmit="OnValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<div>
<label>First name:</label>
<InputText @bind-Value="person!.FirstName"></InputText><ValidationMessage For="@(() => person!.FirstName)"></ValidationMessage>
</div>
<div>
<label>Last name:</label>
<InputText @bind-Value="person!.LastName"></InputText><ValidationMessage For="@(() => person!.LastName)"></ValidationMessage>
</div>
<button type="submit">Submit</button>
</EditForm>
@code {
public class Person
{
[Required] [StringLength(10)]
public string FirstName { get; set; } = "";
[Required]
[StringLength(10)]
public string LastName { get; set; } = "";
}
[SupplyParameterFromForm(FormName = "DataEntryForm")]
private Person? person { get; set; }
protected override Task OnInitializedAsync()
{
person = new();
return base.OnInitializedAsync();
}
private void OnValidSubmit()
{
Console.WriteLine($"Submitted: {person!.FirstName} {person!.LastName}");
}
private void OnInvalidSubmit(EditContext obj)
{
foreach (var message in obj.GetValidationMessages())
{
Console.WriteLine(message);
}
}
}