简体   繁体   English

如何使我的 Blazor 的 EditForm 与 DataAnnotations 一起使用?

[英]How can I make my Blazor's EditForm working with DataAnnotations?

After creating a new project in Blazor WebAssembly, I just modify the index.razor to create a simple EditForm like this:在 Blazor WebAssembly 中创建新项目后,我只需修改index.razor即可创建一个简单的EditForm ,如下所示:

@page "/"

@using System.ComponentModel.DataAnnotations
@using EditFormValidation.Models
<h1>Hello, world!</h1>

Welcome to your new app.

<SurveyPrompt Title="How is Blazor working for you?" />

<EditForm EditContext="context" OnValidSubmit="HandleValidSubmit" OnInvalidSubmit="HandleInvalidSubmit">
    <DataAnnotationsValidator />

    <ValidationSummary />

    <InputText @bind-Value="adresse.Adresse1" />
    <ValidationMessage For="(() => adresse.Adresse1)" />

    <InputText @bind-Value="adresse.Adresse2" />
    <ValidationMessage For="(() => adresse.Adresse2)" />

    <InputText @bind-Value="adresse.Email" />
    <ValidationMessage For="(() => adresse.Email)" />

    <button type="submit">Save</button>
</EditForm>

@code{
    public class Adresse
    {
        [Required]
        public string Adresse1;

        [Required]
        public string Adresse2;

        [Required]
        [EmailAddress]
        public string Email;
    }

    Adresse adresse = new Adresse();
    EditContext context;

    protected override void OnInitialized()
    {
        base.OnInitialized();
        context = new EditContext(adresse);
    }

    private void HandleValidSubmit()
    {
        Console.WriteLine("Submit...");
    }

    private void HandleInvalidSubmit()
    {
        Console.WriteLine("Invalid Submit...");
    }
}

There is no validation summary, no messages and the HandleValidSubmit is call when I press "Save".没有验证摘要,没有消息,并且当我按“保存”时会调用 HandleValidSubmit。 Only style on the input text becomes green when text inside.当文本在里面时,只有输入文本上的样式变为绿色。

My solution here我的解决方案在这里

First off, remove this code:首先,删除此代码:

 protected override void OnAfterRender(bool firstRender)
{
    base.OnAfterRender(firstRender);
    context.Validate();
}

Do the following:请执行下列操作:

  • Add a method name HandleValidSubmit as follows:添加方法名称HandleValidSubmit如下:

     private void HandleValidSubmit() { Console.WriteLine("Submit..."); }

This method is called whenever you click on the submit button your are about to add to your EditForm每当您单击要添加到 EditForm 的提交按钮时,都会调用此方法

<button type="submit">Save</button>

You should also add an attribute named OnValidSubmit to your EditForm, and assign it the value "HandleValidSubmit"您还应该在 EditForm 中添加一个名为 OnValidSubmit 的属性,并将其赋值为“HandleValidSubmit”

The OnValidSubmit event is triggered when you press the "submit" button, and the model is valid, and the event handler HandleValidSubmit is executed and prints to the screen the text "Submit..."当您按下“提交”按钮时触发 OnValidSubmit 事件,并且 model 有效,并且事件处理程序 HandleValidSubmit 被执行并在屏幕上打印文本“提交...”

Here's the complete relevant code snippet:这是完整的相关代码片段:

<EditForm EditContext="context" OnValidSubmit="HandleValidSubmit">
    <DataAnnotationsValidator />

    <ValidationSummary />

    <InputText @bind-Value="adresse.Adresse1" />
    ValidationMessage For="@(() => adresse.Adresse1)" />

    <button type="submit">Save</button>
</EditForm>

@code{
     private void HandleValidSubmit()
      {
           Console.WriteLine("Submit...");
      }
}

Note: the HandleValidSubmit will never be called, as your model will not pass validation, unless you provide values for Adresse2 and Email.注意:永远不会调用 HandleValidSubmit,因为您的 model 不会通过验证,除非您提供 Adresse2 和 Email 的值。 You can do it by adding tag elements for both fields into the EditForm您可以通过将两个字段的标签元素添加到 EditForm

Note: Your model class should be defined with get and set accessors for each property, and without the semi-colon at the end.注意:您的 model class 应该为每个属性定义 get 和 set 访问器,并且末尾没有分号。

public class Adresse
    {
        [Required]
        public string Adresse1  { get; set; }

        [Required]
        public string Adresse2  { get; set; }

        [Required]
        [EmailAddress]
        public string Email  { get; set; }
    }
 

Hope this helps...希望这可以帮助...

The members of the model must be properties. model 的成员必须是属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM