2014-10-17 131 views
0

请帮助,任何人都可以告诉我为什么我的自定义验证没有在我的mvc 4项目中开火。MVC 4验证

这里是我的模型......

[Required(ErrorMessage = "Old Password is required field.")] 
[VerifyOldPassword(ErrorMessage = "Invalid Old Password.")] 
public string PasswordOld { get; set; } 

属性类

public class VerifyOldPasswordAttribute : ValidationAttribute 
{ 
    public VerifyOldPasswordAttribute() 
    { 

    } 
    public override bool IsValid(object value) 
    { 

     string currentValue = value as string; 
     string referrenceValue = mtdGetOldPassword(); 

     if (currentValue != referrenceValue) 
     { 
      return false; 
     } 

     return true; 
    } 
} 

浏览:

@Html.PasswordFor(m => m.PasswordOld, new { @class = "txtInputLong" }) 
@Html.ValidationMessageFor(m => m.PasswordOld, "", 
      new { style = "font-family:Arial;font-size:small;font-weight:bold;color:red" }) 

预先感谢您....

+1

刚刚复制你的代码到我的项目,它工作正常(或者是你希望客户端验证呢?) – 2014-10-17 05:40:01

+1

您创建的自定义验证只是仅适用于服务器端,但不是因为它的客户端没有实现IClientValidatable。当没有提供有效数据时,检查您的ModelState.IsValid属性是否为false。 – Venkat 2014-10-17 06:22:27

+0

@Venkat,我怀疑从OP的代码,客户端验证不会在这里工作,因为它从某个地方(数据库)获取旧值?因此无论是内置[RemoteAttribute](http://msdn.microsoft.com/en-us/library/system.web.mvc.remoteattribute(v = vs.118).aspx)或[CompareAttribute](http: //msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.compareattribute%28v=vs.110%29.aspx)可能是一个更好的解决方案,客户端验证 – 2014-10-17 06:47:11

回答

0

请检查这些设置正确或n ot在你的项目的web.config?

<configuration> 
    <appSettings> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
    </appSettings> 
</configuration> 
+0

是的,它与上面提供的设置相同... – marai 2014-10-17 05:38:17

+0

您是否收到任何错误? JavaScript或jQuery的错误? – Mukund 2014-10-17 05:42:07