如何在vNext中获取客户端验证规则?

时间:2015-06-05 14:16:13

标签: annotations asp.net-core-mvc tag-helpers

来自我的自定义标记助手:

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
        }**

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

Validation.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;

    }

}

0 个答案:

没有答案