3

从我的自定义标签助手:

TagHelper.cs
public class CustomDropDownListTagHelper : TagHelper
    {
        [HtmlAttributeName("for")]
        public ModelExpression For { get; set; }

        [HtmlAttributeName("items")]
        public IEnumerable<SelectListItem> Items { get; set; }

        **public override void Process(TagHelperContext context, TagHelperOutput output)
        { 
            ...Get client validation rules to render
        }**

如何从自定义验证数据注释中获取客户端验证规则?

验证.cs

public class CustomRequiredAttribute : ValidationAttribute, IClientModelValidator
{

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
      ...
    }


    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ClientModelValidationContext context)
    {
        var rule = new ModelClientValidationRule("required", "This field is required");
        yield return rule;

    }

}
4

0 回答 0