blazor - onvalidsubmit 显示错误消息但触发 onvalidsubmitcode

blazor - onvalidsubmit shows error message but triggers onvalidsubmitcode

我正在试用一个带有 blazor 的小应用程序。该代码有一个 class 和编辑表单。编辑表单显示无效数据错误,但仍运行提交代码。

我现在只测试表单中的 2 个字段以供学习。

Class 数据注释:

 public class DBPersonDetails
    {

        [Required]
        [Display(Name = "Client ID")]
        public UInt16 Client_ID { get; set; }

        [Required]
        [Display(Name = "Branch ID")]
        public UInt16 Branch_ID { get; set; }

        [Required]
        [Display(Name = "Person ID")]
        public UInt32 Person_ID { get; set; }

        [Required]
        [StringLength(100)]
        [Display(Name = "Person Title")]
        public string PersonTitle { get; set; }

        [Required]
        [Display(Name = "Person Type ID")]
        public Lookup_Person_Type Person_Type_ID { get; set; }

        [Required]
        [Display(Name = "Person Gender ID")]
        public Lookup_Person_Gender Person_Gender_ID { get; set; }

        
        [StringLength(100)]
        [EmailAddress]
        [DataType(DataType.EmailAddress)]        
        [Display(Name = "Person Email")]
        public string? Person_Email { get; set; }

        [StringLength(15)]
        [Display(Name = "Person Mobile")]
        public string? Person_Mobile { get; set; }

        [Display(Name = "Person Dob")]
        public DateTime? Person_Dob { get; set; }

    }

public class DBPeople
{
    public DBPersonDetails dbPersonDetails { get; set; } = new DBPersonDetails();

    public DBPersonAddress dbPersonAddress { get; set; } = new DBPersonAddress();

    public DBPersonProofs DBPersonProofs { get; set; } = new DBPersonProofs();

}

Blazor 页面代码:

@page "/person"

<h3>Person</h3>

<h3>contact Add Page</h3>

<EditForm Model="@person" OnValidSubmit="@onvalidsubmitcode" >
    <DataAnnotationsValidator />
    <ValidationSummary />

    <div class="form-group">
        <label>Title:</label>
        <InputText @bind-Value="person.dbPersonDetails.PersonTitle" />
    </div>

    <div class="form-group">
        <label>Email:</label>
        <InputText @bind-Value="person.dbPersonDetails.Person_Email" />
    </div>

    <button type="submit" class="btn-dark">Submit</button>
</EditForm>
    

@code {

    //public Models.Person person { get; set; } = new Models.Person();

    public Data.People.DBPeople person { get; set; } = new Data.People.DBPeople();

    private void onvalidsubmitcode()
    {
        
        var k = person;

    }

}

我检查了 onvalidsubmitcode 上的断点,但即使数据无效,它也会被触发。

输入的数据示例:

虽然显示验证错误代码仍然点击提交代码。请帮我看看我犯了什么错误或我有什么missed/messed。

您的编辑上下文是人物。输入绑定到 dbPersonDetails。 'person' 是 DbPeople 类型并且正在通过验证。你需要设置 DBPersonDetails

实例的模型参数