[英]MVC3 Unobtrusive Validation not working for custom DataAnnotations attribute
I have a custom attribute that is currently a simple wrapper of the DataAnnotations.RequiredAttribute (I will extend it later, but just trying to get this proof of concept working for now). 我有一个自定义属性,该属性目前是DataAnnotations.RequiredAttribute的简单包装(我将在以后扩展它,但仅是尝试使这种概念证明目前有效)。 However, this isn't working with MVC3 unobtrusive validation.
但是,这不适用于MVC3非侵入式验证。
It's a very simple problem, really. 确实,这是一个非常简单的问题。
Here is my custom attribute: 这是我的自定义属性:
[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;
}
}
Here are two model properties, one using the custom attribute, one using the DataAnnotations attribute: 这是两个模型属性,一个使用custom属性,一个使用DataAnnotations属性:
[System.ComponentModel.DataAnnotations.Required]
public string FirstName { get; set; }
[CustomValidationAttributes.Required]
public string LastName { get; set; }
Here is the Razor code: 这是剃刀代码:
<p>
@Html.TextBoxFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</p>
<p>
@Html.TextBoxFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</p>
And here is the resulting output: 这是结果输出:
<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>
So as you can see, FirstName (using DataAnnotations) is rendered with the necessary html attributes needed for the validators, but LastName (using CustomValidationAttributes) is missing the data-val-required
and data-val attributes
. 如您所见,FirstName(使用DataAnnotations)呈现有验证器所需的必要html属性,而LastName(使用CustomValidationAttributes)缺少
data-val-required
和data-val attributes
。
Am I doing something wrong, or is this not supported with MVC3 unobtrusive validation? 我是在做错什么,还是MVC3非侵入式验证不支持此功能?
Thanks in advance. 提前致谢。
As ingo pointed out above in the comments, I ended up having to implement IClientValidatable
in order for these to work. 正如上面的ingo在评论中指出的那样,我最终不得不实现
IClientValidatable
才能使它们起作用。 So, in my example above, I had to add this to my attribute: 因此,在上面的示例中,我必须将其添加到我的属性中:
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var modelClientValidationRule = new ModelClientValidationRule
{
ErrorMessage = FormatErrorMessage(metadata.DisplayName),
ValidationType = "required"
};
yield return modelClientValidationRule;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.