【发布时间】:2019-11-15 12:26:13
【问题描述】:
我这样定义了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 应用作为教程,它运行良好。
【问题讨论】:
标签: c# asp.net asp.net-core blazor-server-side