我这样定义了LogInForm.cs
表单:
using System.ComponentModel.DataAnnotations;
namespace PollingInstitute.Data.LogIn
{
public class LogInForm
{
[Required]
[DataType(DataType.Emailaddress,ErrorMessage = "This field require a mail adress (example : abc@xyz.com)")]
public string Mail { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
}
}
还有这样的登录屏幕LogIn.razor
:
@page "/login"
@using PollingInstitute.Data.LogIn
@inject LogInService _LogInService
<h3>Login</h3>
<EditForm Model="@logInForm" OnValidSubmit="@TryLogIn">
<div>
<label>Mail Adress : </label><InputText @bind-Value="@logInForm.Mail"></InputText>
<Validationmessage For="@(()=>logInForm.Mail)"></Validationmessage>
</div>
<div>
<label>Password : </label><InputText @bind-Value="@logInForm.Password"></InputText>
<Validationmessage For="@(()=>logInForm.Password)"></Validationmessage>
</div>
<div>
@if (!IsDisabled)
{
<button type="submit">Submit</button>
}
else
{
<label>Please wait...</label>
}
</div>
<ValidationSummary></ValidationSummary>
</EditForm>
@code {
LogInForm logInForm = new LogInForm();
protected bool IsDisabled { get; set; } = false;
async void TryLogIn()
{
if (IsDisabled == true)
{
IsDisabled = true;
StateHasChanged();
bool result = (await _LogInService.TryLogIn(logInForm));
Console.WriteLine("Logging status : " + (result ? "Sucess" : "Failure"));
IsDisabled = false;
StateHasChanged();
}
}
}
当我填写(或不填写)这些字段时,即使它被认为是无效的,也总是表明它们是有效的。
我检查了_Imports.razor
,它得到了microsoft.AspNetCore.Components.Forms
库。
我尝试过使用Chrome和Firefox,但始终能得到相同的结果。
我检查了javascript是否已启用。
那我做错了什么?我的代码有什么关系吗?是否需要在Startup.cs
文件中添加一些内容?我使用验证系统制作了Blazor应用程序作为教程,并且运行正常。