C#C
C#17mo ago
Jelles

Blazor Page not updating model

Hi, I want to create a login page but I am having issues getting the data to the backend. My backend is working I am using Aspnetcore identity for authentication. The endpoint /login and /register work fine in postman when testing. But when I use my login page I get an error. I have been trying to figure out why this is happening and I feel like the data from my form is not being updated to my model. When debugging this code I see that loginModel is never updated and the values are always null. I left out the authentication code for debugging code. Here is my code:

C#
@page "/login"
@inject HttpClient Http
@using System.Diagnostics

<div class="text-center">
    <EditForm class="form-signin mb-4" FormName="Login" Model="@loginModel" OnSubmit="@Authenticate">
        <div class="form-group">
            <label for="user-name">Email</label>
            <InputText class="form-control" id="user-name" aria-describedby="emailHelp" placeholder="E-mail" @bind-Value="@loginModel.Email" />
        </div>
        <div class="form-group">
            <label for="password">Password</label>
            <InputText type="password" class="form-control" id="password" placeholder="Password" @bind-Value="@loginModel.Password" />
        </div>
        <div class="form-group">
            <button type="submit" id="login" name="login" class="btn btn-primary">Login</button>
        </div>
    </EditForm>
</div>

@if (!string.IsNullOrEmpty(message))
{
    <p>@message</p>
}

@code {
    public string message;
    public string url = "http://localhost:5083";
    public LoginModel loginModel = new LoginModel();

    private void Authenticate()
    {
        Console.WriteLine("Email:" + loginModel.Email);
        Console.WriteLine("Password:" + loginModel.Password);
    }
    
    public class LoginModel
    {
        public string Email { get; set; }
        public string Password { get; set; }
    }
}
Was this page helpful?