使用 DataAnnotations 或 CustomValidation 验证模型的子属性
我正在开发一个 MVC3 Web 应用程序,我的模型由许多使用自定义类型作为文本属性的类组成。 例如,我的 Product 实体看起来像:
public class Product
{
public int ProductId {get;set;}
public TextRef Title {get;set;}
public TextRef Description {get;set;}
}
而 TextRef 是:
public class TextRef
{
public int LangId {get;set;}
public string Text {get;set;}
}
所以在我看来,我正在使用:
@Html.EditorFor(model => model.Title.Text)
@Html.ValidationMessageFor(model => model.Title.Text)
有没有办法使用 DataAnnotations 启用这些 TextRef 属性的验证?
我没有找到任何,所以我创建了一个自定义验证器属性:
public class TextRefRequiredAttribute : ValidationAttribute, IClientValidatable
{
public override bool IsValid(object value)
{
if (value is TextRef)
return !string.IsNullOrEmpty(((TextRef)value).Text);
else
return true;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule();
rule.ValidationType = "textref";
yield return rule;
}
}
现在服务器端验证正在工作,但客户端却没有。事实上,“data-val-”属性并未为其输入生成。
更新:
我的客户端脚本是:
$.validator.addMethod("textref", function (value, element, param) {
return (value != "");
});
$.validator.unobtrusive.adapters.addBool("textref");
我也尝试使用 FluentValidation:
public class CategoryValidator : AbstractValidator<CategoryViewModel>
{
public CategoryValidator()
{
RuleFor(o => o.Title.Text)
.NotEmpty();
}
}
但仍然没有机会。
谢谢
i'm working on a MVC3 webapp and my Model consist of many classes that use a custom type for textual properties.
for example my Product entity looks like :
public class Product
{
public int ProductId {get;set;}
public TextRef Title {get;set;}
public TextRef Description {get;set;}
}
and TextRef is :
public class TextRef
{
public int LangId {get;set;}
public string Text {get;set;}
}
so in my views i'm using :
@Html.EditorFor(model => model.Title.Text)
@Html.ValidationMessageFor(model => model.Title.Text)
is there a way to enable validation for these TextRef properties using DataAnnotations ?
i didn't find any, so i have created a custom validator attribute:
public class TextRefRequiredAttribute : ValidationAttribute, IClientValidatable
{
public override bool IsValid(object value)
{
if (value is TextRef)
return !string.IsNullOrEmpty(((TextRef)value).Text);
else
return true;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule();
rule.ValidationType = "textref";
yield return rule;
}
}
now the server-side validation is working but client-side isn't. in fact the "data-val-" attributes didn't generated for their inputs.
UPDATE:
my client script is :
$.validator.addMethod("textref", function (value, element, param) {
return (value != "");
});
$.validator.unobtrusive.adapters.addBool("textref");
i also tried using FluentValidation:
public class CategoryValidator : AbstractValidator<CategoryViewModel>
{
public CategoryValidator()
{
RuleFor(o => o.Title.Text)
.NotEmpty();
}
}
but still no chance.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该属性对我来说看起来不错。您需要在客户端注册自定义适配器,不过它看起来应该有点像这样。
上面会出现
The attribute looks ok to me. You will need to register the custom adapter at the client side though which should look a bit like this.
Which will appear above