Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor Server EditForm Model Initialize Net 8

$
0
0

With a background a long time ago in Razor MVC 5 I'm playing around with Blazor after using Angular 12+ for the recent years.

I'm struggling with EditForm Submit - only a simple application but it isn't behaving as I expected and I'm wondering what OnInitialized is doing.

The CreateUserDto I generated through NSwag and all of the fields have the required attribute.

The form that is being edited only contains four of the six properties, so they are not getting bound to within the EditForm.

Instead, as I don't want the user to have control over these, I initialised the properties in the OnInitialize() override method.

However when I populate the fields within the form and submit, I get errors saying that Username and Role are required.

If I remove OnValidSubmit and just use Submit, I can see that there properties are null, but I am unsure why.

Any help would be great, thanks.

Code is below:

@page "/users/register"@inject IClient HttpClient@inject NavigationManager _navManager;<h3>Register New User</h3>@if (!string.IsNullOrEmpty(message)){<div class="alert-danger"><p>@message</p></div>}<div class="card-body"><EditForm EditContext="_editContext" OnValidSubmit="HandleRegistration" FormName="Register">        @* Enforces validation *@<DataAnnotationsValidator></DataAnnotationsValidator>        @* Prints out anything thats invalid *@<ValidationSummary></ValidationSummary><div class="form-group"><label>Email Address</label><InputText class="form-control" @bind-Value="_registrationModel!.Email"></InputText><ValidationMessage For="() => _registrationModel.Email"></ValidationMessage></div><div class="form-group"><label>First Name</label><InputText class="form-control" @bind-Value="_registrationModel!.FirstName"></InputText><ValidationMessage For="() => _registrationModel.FirstName"></ValidationMessage></div><div class="form-group"><label>Last Name</label><InputText class="form-control" @bind-Value="_registrationModel!.LastName"></InputText><ValidationMessage For="() => _registrationModel.LastName"></ValidationMessage></div><div class="form-group"><label>Password</label><InputText class="form-control" @bind-Value="_registrationModel!.Password"></InputText><ValidationMessage For="() => _registrationModel.Password"></ValidationMessage></div><button type="submit" class="btn btn-primary btn-block" >Register</button></EditForm></div>@code {    private EditContext? _editContext;    [SupplyParameterFromForm]    public CreateUserDto? _registrationModel { get; set; }    protected override async Task OnInitializedAsync()    {        _registrationModel ??= new CreateUserDto        {            Username = string.Empty,            Role = "User"        };        _editContext = new EditContext(_registrationModel);    }    string message = String.Empty;    private async Task HandleRegistration()    {        if (_registrationModel != null)        {            _registrationModel.Username = _registrationModel.Email;            _registrationModel.Role = "User";        }        try        {            await HttpClient.RegisterAsync(_registrationModel);            _navManager.NavigateTo("'/users/login");        }        catch (ApiException ae)        {            message = ae.Message;        }        catch (Exception e)        {            message = e.Message;        }    }}

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>