【问题标题】:blazor - onvalidsubmit shows error message but triggers onvalidsubmitcodeblazor - onvalidsubmit 显示错误消息但触发 onvalidsubmitcode
【发布时间】:2020-07-05 14:34:59
【问题描述】:

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

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

类数据注释:

 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 上的断点,但即使数据无效,它也会被触发。

输入数据示例:

虽然显示了验证错误,但代码仍然会点击提交代码。请帮助我解决我犯了什么错误或我错过/搞砸了什么。

【问题讨论】:

    标签: c# asp.net-core blazor asp.net-core-3.1


    【解决方案1】:

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

    【讨论】:

    猜你喜欢
    • 2012-08-28
    • 1970-01-01
    • 2019-11-13
    • 2014-11-30
    • 2020-12-28
    • 2020-01-21
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    相关资源
    最近更新 更多