2

我正在使用 blazore 服务器端。使用元数据类时表单验证不起作用。我正在使用脚手架,我的数据库模型通常是自动生成的。如果我修改它,它会清除我所有的 Dataannotation 验证。

这是我正在使用的课程

public partial class RequestHelp
{
    public Guid ID { get; set; }
    public Guid UserID { get; set; }
    public string HelpFor { get; set; }
    public string TypeOfHelp { get; set; }
    public string Description { get; set; }
    public DateTime UTCDateCreated { get; set; }
    public DateTime UTCLastUpdateDate { get; set; }
    public string CurrencyCode { get; set; }
    public decimal? Amount { get; set; }
    public bool? IsDeleted { get; set; }
}



[MetadataType(typeof(RequestHelpMetaData))]
public partial class RequestHelp
{

}
public class RequestHelpMetaData
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid ID { get; set; }

    [Required(ErrorMessage = "Please select Who needs Help.")]
    public string HelpFor { get; set; }

    [Required(ErrorMessage = "Please select What kind of Help you are Requesting.")]
    public string TypeOfHelp { get; set; }

    [Required(ErrorMessage = "Please Currency.")]
    public string CurrencyCode { get; set; }

    [Required(ErrorMessage = "Amount Field is required")]
    [Range(1, 999999999999999, ErrorMessage = "Please enter positive number")]
    public decimal? Amount { get; set; }

    [Required(ErrorMessage = "Please enter Details about what you need help for.")]
    [StringLength(4000,MinimumLength=10, ErrorMessage = "Please eneter some details about the help you need")]
    public string Description { get; set; }


}
}

这就是我在 blazor 中使用它的方式。

<EditForm Model="@_requesForHelp" OnValidSubmit="@HandleValidSubmit">
                <DataAnnotationsValidator />

                <ValidationSummary />
 </EditForm>
4

0 回答 0