ASP .NET MVC 3 - RemoteAttribute覆盖IsValid的,获得了额外的字段 [英] ASP .NET MVC 3 - RemoteAttribute override IsValid, access to the additional fields
问题描述
伙计们,我创建一个新的属性,它inhetirs从System.Web.Mvc.RemoteAttribute。我想重写方法IsValid为验证也适用,而不启用JavaScript。但我不知道我可以存取权限的其他字段。我的code是:
[AttributeUsage(AttributeTargets.Property)
公共类DocumentationAttribute:System.Web.Mvc.RemoteAttribute
{
公共DocumentationAttribute()
:基地(ValidPass,验证)
{
base.HttpMethod =邮报;
}
私人DocumentationAttribute(字符串routeName)
:基地(routeName)
{
}
私人DocumentationAttribute(串动,串器)
:基座(动作,控制器)
{
}
私人DocumentationAttribute(串动,串控制器,串AREANAME)
:基地(动作,控制器,AREANAME)
{
}
公众覆盖布尔的IsValid(对象的值)
{
//我如何可以访问到更多的领域?
}}
类模型:
[显示(名称为护照)
[文件(的ErrorMessage ={0}无效,AdditionalFields =IDPass)
公共虚拟字符串护照
{
组;
得到;
}
没有JavaScript的你不能这样做远程验证。这就是它是如何工作的。你可能也仅仅只依靠服务器端验证,因为这是唯一一种验证的工作没有JavaScript的。
您想创建一个远程工作的JavaScript的验证,但确实服务器端,如果禁用JavaScript?如果是这样,那么你应该创建一个从ValidationAttribute派生的自定义属性,并使用远程的。
针对您的问题,您需要创建在名为属性AdditionalFields一个公共属性,这个ID在初始设置时申报财产,那么你可以从你的IsValid访问属性。
编辑:
根据您的更多信息,你需要使用的IsValid的第二种形式,是以验证环境。你会做这样的事情:
保护覆盖的ValidationResult的IsValid(对象的值,ValidationContext validationContext)
{
VAR栏= validationContext.ObjectType.GetProperty(this.AdditionalFields);
VAR fieldsValue =(字符串)fields.GetValue(validationContext.ObjectInstance,NULL);
}
Folks, i am creating a new attribute which inhetirs from System.Web.Mvc.RemoteAttribute. I would like to override the method IsValid for that the validation also works without the enabled JavaScript. But i don't know how i can acces to the additional fields. My code is:
[AttributeUsage(AttributeTargets.Property)]
public class DocumentationAttribute : System.Web.Mvc.RemoteAttribute
{
public DocumentationAttribute()
: base("ValidPass", "Validation")
{
base.HttpMethod = "Post";
}
private DocumentationAttribute(string routeName)
: base(routeName)
{
}
private DocumentationAttribute(string action, string controller)
: base(action, controller)
{
}
private DocumentationAttribute(string action, string controller, string areaName)
: base(action, controller, areaName)
{
}
public override bool IsValid(object value)
{
//How can i access to the additional fields?
}}
Class Model:
[Display(Name = "Passport")]
[Documentation(ErrorMessage = "{0} not valid", AdditionalFields = "IDPass")]
public virtual String Passport
{
set;
get;
}
You can't do remote validation without javascript. That's how it works. You might as well just rely on server-side validation only, because that's the only kind of validation that works without javascript.
Are you trying to create a validator that works remotely with javascript, but does serverside if javascript is disabled? If so, then you should create a custom attribute derived from ValidationAttribute, and use remote as well.
Specifically for your question, you need to create a public property in your attribute called AdditionalFields, this gets set in the initializer when you declare the property, then you can access the property from your IsValid.
EDIT:
Based on your additional information, you need to use the second form of IsValid that takes the validation context. You would do something like this:
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var fields = validationContext.ObjectType.GetProperty(this.AdditionalFields);
var fieldsValue = (string)fields.GetValue(validationContext.ObjectInstance, null);
}
这篇关于ASP .NET MVC 3 - RemoteAttribute覆盖IsValid的,获得了额外的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!