C
C#4d ago
Dani

most simple form (impossible)

want to do the most basic of basic thing and thats to send a form and get the value of it right now i got this page:
@page "/Register"
@using Microsoft.AspNetCore.Identity
@using System.ComponentModel.DataAnnotations
@using learning.Models

<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-header">
<h3 class="text-center">Register</h3>
<h1>Status: @status</h1>
</div>
<div class="card-body">
<EditForm Model="@registerModel" OnValidSubmit="@HandleValidSubmit" FormName="registerModel">
<DataAnnotationsValidator />
<ValidationSummary />

<div class="form-group">
<label for="email">Email</label>
<InputText class="form-control" @bind-Value="registerModel.Email" />
</div>

<div class="form-group">
<label for="password">Password</label>
<InputText type="password" class="form-control" @bind-Value="registerModel.Password" />
</div>

<div class="form-group">
<label for="confirmPassword">Confirm Password</label>
<InputText type="password" class="form-control" @bind-Value="registerModel.ConfirmPassword" />
</div>

<button type="submit" class="btn btn-primary btn-block mt-3">Register</button>
</EditForm>

<div class="mt-3 text-center">
<a href="/account/login">Already have an account? Login here</a>
</div>
</div>
</div>
</div>
</div>
</div>
@page "/Register"
@using Microsoft.AspNetCore.Identity
@using System.ComponentModel.DataAnnotations
@using learning.Models

<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-header">
<h3 class="text-center">Register</h3>
<h1>Status: @status</h1>
</div>
<div class="card-body">
<EditForm Model="@registerModel" OnValidSubmit="@HandleValidSubmit" FormName="registerModel">
<DataAnnotationsValidator />
<ValidationSummary />

<div class="form-group">
<label for="email">Email</label>
<InputText class="form-control" @bind-Value="registerModel.Email" />
</div>

<div class="form-group">
<label for="password">Password</label>
<InputText type="password" class="form-control" @bind-Value="registerModel.Password" />
</div>

<div class="form-group">
<label for="confirmPassword">Confirm Password</label>
<InputText type="password" class="form-control" @bind-Value="registerModel.ConfirmPassword" />
</div>

<button type="submit" class="btn btn-primary btn-block mt-3">Register</button>
</EditForm>

<div class="mt-3 text-center">
<a href="/account/login">Already have an account? Login here</a>
</div>
</div>
</div>
</div>
</div>
</div>
3 Replies
Dani
DaniOP4d ago
the code with this page (couldnt post it because it was too long)
@code {
private RegisterModel registerModel = new();
private string status = "No Status";

private void HandleValidSubmit()
{
status = $"Successfully registered: {registerModel.Email}";
Console.WriteLine($"Form submitted with email: {registerModel.Email}");
}
}
@code {
private RegisterModel registerModel = new();
private string status = "No Status";

private void HandleValidSubmit()
{
status = $"Successfully registered: {registerModel.Email}";
Console.WriteLine($"Form submitted with email: {registerModel.Email}");
}
}
and then the register model is this
using System.ComponentModel.DataAnnotations;

namespace learning.Models
{
public class RegisterModel
{
[Required(ErrorMessage = "Email is required.")]
[EmailAddress(ErrorMessage = "Invalid email address.")]
public string Email { get; set; } = "";

[Required(ErrorMessage = "Password is required.")]
[StringLength(100, MinimumLength = 6, ErrorMessage = "Password must be at least 7 characters.")]
public string Password { get; set; } = "";

[Required(ErrorMessage = "Please confirm your password.")]
[Compare(nameof(Password), ErrorMessage = "Passwords do not match.")]
public string ConfirmPassword { get; set; } = "";
}
}
using System.ComponentModel.DataAnnotations;

namespace learning.Models
{
public class RegisterModel
{
[Required(ErrorMessage = "Email is required.")]
[EmailAddress(ErrorMessage = "Invalid email address.")]
public string Email { get; set; } = "";

[Required(ErrorMessage = "Password is required.")]
[StringLength(100, MinimumLength = 6, ErrorMessage = "Password must be at least 7 characters.")]
public string Password { get; set; } = "";

[Required(ErrorMessage = "Please confirm your password.")]
[Compare(nameof(Password), ErrorMessage = "Passwords do not match.")]
public string ConfirmPassword { get; set; } = "";
}
}
whenever i submit the form i see in the network console the post request with the value but the status never shows the value and i always get the errormessage so what is it that a simple thing as getting a form value is just not working? .net 9 blazor server side
becquerel
becquerel3d ago
you probably need to call StateHasChanged() at the end of your HandleValidSubmit method blazor does not know your field has changed its value unless you tell it
Dani
DaniOP3d ago
nope that did not change anything still get this result
No description

Did you find this page helpful?