【问题标题】:MVC3 Unobtrusive Validation not working for custom DataAnnotations attributeMVC3 Unobtrusive Validation 不适用于自定义 DataAnnotations 属性
【发布时间】:2011-10-22 18:12:45
【问题描述】:

我有一个自定义属性,它目前是 DataAnnotations.RequiredAttribute 的简单包装(稍后我将对其进行扩展,但现在只是试图让这个概念证明工作)。但是,这不适用于 MVC3 不显眼的验证。

这是一个非常简单的问题,真的。

这是我的自定义属性:

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class RequiredAttribute : System.ComponentModel.DataAnnotations.RequiredAttribute
{
    public RequiredAttribute()
    {
    }

    public RequiredAttribute(Type errorMessageResourceType, string errorMessageResourceName)
    {
        this.ErrorMessageResourceName = errorMessageResourceName;
        this.ErrorMessageResourceType = errorMessageResourceType;
    }
}

这里有两个模型属性,一个使用自定义属性,一个使用 DataAnnotations 属性:

[System.ComponentModel.DataAnnotations.Required]
public string FirstName { get; set; }

[CustomValidationAttributes.Required]
public string LastName { get; set; }

这是 Razor 代码:

<p>
    @Html.TextBoxFor(model => model.FirstName)
    @Html.ValidationMessageFor(model => model.FirstName)
</p>
<p>
    @Html.TextBoxFor(model => model.LastName)
    @Html.ValidationMessageFor(model => model.LastName)
</p>

这是结果输出:

<p>
    <input type="text" value="" name="FirstName id="FirstName" data-val-required="The First Name field is required." data-val="true">
    <span data-valmsg-replace="true" data-valmsg-for="FirstName" class="field-validation-valid"></span>
</p>
<p>
    <input type="text" value="" name="LastName" id="LastName">
    <span data-valmsg-replace="true" data-valmsg-for="LastName" class="field-validation-valid"></span>
</p>

如您所见,FirstName(使用 DataAnnotations)使用验证器所需的必要 html 属性呈现,但 LastName(使用 CustomValidationAttributes)缺少 data-val-requireddata-val attributes

我做错了什么,还是 MVC3 不显眼的验证不支持?

提前致谢。

【问题讨论】:

  • 你可以在这里找到你的解决方案stackoverflow.com/questions/6495510/…
  • @ingo - 不过我很困惑。如果我不扩展基本验证,为什么我必须通过实现IsValidGetClientValidationRules 来“重新发明轮子”,如果这些实现已经存在并且适用于基本验证属性(在这种情况下为RequiredAttribute)?

标签: c# asp.net-mvc asp.net-mvc-3 data-annotations unobtrusive-validation


【解决方案1】:

正如 ingo 在上面在 cmets 中指出的那样,我最终不得不实现 IClientValidatable 才能使这些工作。因此,在上面的示例中,我必须将其添加到我的属性中:

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var modelClientValidationRule = new ModelClientValidationRule
        {
            ErrorMessage = FormatErrorMessage(metadata.DisplayName),
            ValidationType = "required"
        };

        yield return modelClientValidationRule;
    }

【讨论】:

  • 我刚刚写了一篇关于创建自定义 ValidationAttribute 以及如何将其转换为不显眼的验证的博客。该示例位于github 上,并与我编写的属性一起使用,该属性要求您的字符串属性包含指定的单词或短语。您还可以使用RequiredAttributeAdapter see here 注册您的自定义RequiredAttribute
  • @JimSchubert 你能链接到博客文章吗?
  • @AnnL。该帖子链接在 github 页面的摘要中。在这里,以防万一:ipreferjim.com/?p=606
猜你喜欢
  • 2020-11-18
  • 2015-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-30
  • 2016-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多