Unable to set property 'onsubmit' on object of type
Hi everyone! I created a simple login page but it throws me an exception:
Here's my blazor page:
Here's LoginUser class:
What am I doing wrong?
InvalidOperationException: Unable to set property 'onsubmit' on object of type 'Microsoft.AspNetCore.Components.Forms.EditForm'. The error was: Unable to cast object of type 'Microsoft.AspNetCore.Components.EventCallback`1[System.EventArgs]' to type 'Microsoft.AspNetCore.Components.EventCallback`1[Microsoft.AspNetCore.Components.Forms.EditContext]'.
InvalidOperationException: Unable to set property 'onsubmit' on object of type 'Microsoft.AspNetCore.Components.Forms.EditForm'. The error was: Unable to cast object of type 'Microsoft.AspNetCore.Components.EventCallback`1[System.EventArgs]' to type 'Microsoft.AspNetCore.Components.EventCallback`1[Microsoft.AspNetCore.Components.Forms.EditContext]'.
@page "/Login"
@using System.Text.Json
@using System.Net
@using Novel.Blazor.Models
@inject HttpClient HttpClient
@inject ProtectedSessionStorage ProtectedSessionStore
<h3>Login</h3>
@rendermode InteractiveServer
<div>
<EditForm method="post" Model="@LoginUser" @onsubmit="@Submit">
<InputText type="email" @bind-Value="LoginUser.Email"></InputText>
<InputText type="password" @bind-Value="LoginUser.Password"></InputText>
<button type="submit">Login</button>
</EditForm>
</div>
@code {
[SupplyParameterFromForm]
private LoginUser? LoginUser { get; set; }
protected override void OnInitialized() => LoginUser ??= new LoginUser();
[Inject] ModalService ModalService { get; set; } = default!;
private async Task Submit()
{
var serialized = JsonSerializer.Serialize(LoginUser);
var result = await HttpClient.PostAsJsonAsync($"api/User", serialized).ConfigureAwait(true);
if (result.IsSuccessStatusCode)
{
await using var responseStream = await result.Content.ReadAsStreamAsync();
var token = await JsonSerializer.DeserializeAsync
<TokenDto>(responseStream);
await ProtectedSessionStore.SetAsync("token", token!.Token);
}
else
{
if (result.StatusCode == HttpStatusCode.Unauthorized)
{
await ShowError("Email or/and password are incorrect!");
}
else
{
await ShowError("Something went wrong");
}
}
}
private async Task ShowError(string message)
{
var modalOption = new ModalOption
{
Title = "Modal title",
Message = message,
Type = ModalType.Danger,
Size = ModalSize.Regular
};
await ModalService.ShowAsync(modalOption);
}
}
@page "/Login"
@using System.Text.Json
@using System.Net
@using Novel.Blazor.Models
@inject HttpClient HttpClient
@inject ProtectedSessionStorage ProtectedSessionStore
<h3>Login</h3>
@rendermode InteractiveServer
<div>
<EditForm method="post" Model="@LoginUser" @onsubmit="@Submit">
<InputText type="email" @bind-Value="LoginUser.Email"></InputText>
<InputText type="password" @bind-Value="LoginUser.Password"></InputText>
<button type="submit">Login</button>
</EditForm>
</div>
@code {
[SupplyParameterFromForm]
private LoginUser? LoginUser { get; set; }
protected override void OnInitialized() => LoginUser ??= new LoginUser();
[Inject] ModalService ModalService { get; set; } = default!;
private async Task Submit()
{
var serialized = JsonSerializer.Serialize(LoginUser);
var result = await HttpClient.PostAsJsonAsync($"api/User", serialized).ConfigureAwait(true);
if (result.IsSuccessStatusCode)
{
await using var responseStream = await result.Content.ReadAsStreamAsync();
var token = await JsonSerializer.DeserializeAsync
<TokenDto>(responseStream);
await ProtectedSessionStore.SetAsync("token", token!.Token);
}
else
{
if (result.StatusCode == HttpStatusCode.Unauthorized)
{
await ShowError("Email or/and password are incorrect!");
}
else
{
await ShowError("Something went wrong");
}
}
}
private async Task ShowError(string message)
{
var modalOption = new ModalOption
{
Title = "Modal title",
Message = message,
Type = ModalType.Danger,
Size = ModalSize.Regular
};
await ModalService.ShowAsync(modalOption);
}
}
public class LoginUser
{
public string Email { get; set; }
public string Password { get; set; }
};
public class LoginUser
{
public string Email { get; set; }
public string Password { get; set; }
};
4 Replies