1
伙计们,我创建一个从System.Web.Mvc.RemoteAttribute获取的新属性。我想重写方法IsValid,验证也可以在没有启用JavaScript的情况下工作。但我不知道我可以如何使用其他字段。我的代码是:ASP .NET MVC 3 - RemoteAttribute覆盖IsValid,访问其他字段
[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?
}}
类模型:
[Display(Name = "Passport")]
[Documentation(ErrorMessage = "{0} not valid", AdditionalFields = "IDPass")]
public virtual String Passport
{
set;
get;
}
感谢您的回应。我是新手,所以我不知道这是最好的选择。我的应用程序必须是可访问的,我想验证用户的身份证号码。我有一个图书馆的c#这个验证的功能。我想实施两种验证:客户端或服务器。那么,我是否选择了使用remoteattribute进行验证的最佳选项?我不明白你的意思是关于创建派生自ValidationAttribute的自定义属性并使用远程以及...你是否如此愉快发送示例代码? – rspaz16
关于remoteattribute,我试过你的解决方案,但我只能得到该字段的名称,但没有价值。我的模型有一个实体POCO,元数据和信息以及一些SelectList对象。所以我需要在控制器的操作中使用一个前缀来调用远程验证。 我希望你能打消我:) 谢谢! – rspaz16
'[HttpPost] 公共的ActionResult ValidPass([装订(前缀= “Solicitud.DocIdentidad”)]串DocIdentidad,[装订(前缀= “Solicitud.IdTipoDocIdentidad”)] INT?IdTipoDocIdentidad) { 如果(string.IsNullOrEmpty( DocIdentidad)||!IdTipoDocIdentidad.HasValue) { return Json(false); } return Json(Validaciones.IsValidoNIFNIE(DocIdentidad,(Enumerados.TipoDocumentacion)IdTipoDocIdentidad)); }' – rspaz16