注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

网事

备忘录

 
 
 

日志

 
 

Validator Toolkit for ASP.NET MVC  

2010-01-22 16:10:28|  分类: .Net |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

The Validator Toolkit provides a set of validators for the ASP.NET MVC framework to validate HTML forms on the client and server-side using validation sets. By defining a validation set, e.g. a LoginValidationSet class, the toolkit will generate code.

More documentation take a look at the article on CodeProject.com: http://www.codeproject.com/KB/aspnet/MvcValidatorToolkit.aspx

Validator Toolkit for ASP.NET MVC - 夏 - 网事

More Screenshots here on this page.

Basically, you will create a validation set class which derives from ValidationSet base class:

public class LoginValidationSet : ValidationSet{

string Username = "";

string Password = "";

 protected override ValidatorCollection GetValidators() {

return new ValidatorCollection (

 new ValidateElement("username") { Required = true, MinLength = 5, MaxLength = 10 },

new ValidateElement("password") { Required = true, MinLength = 3, MaxLength = 10 } );

}

 protected override void OnValidate() {

if(Username == "Bill" && Password == "Jobs")

 throw new ValidatorException("username", "The username/password combination is not valid");

 }

  }

Then, you will attach it to the view and the HTML form processing action using the ValidationSetAttribute:

public void Login(){

 RenderView("Login");

}

[ValidationSet(typeof(LoginValidationSet))]

public void Authenticate(){

 if(this.ValidateForm()) RenderView("Ok");

else RenderView("Login");}

[ValidationSet(typeof(LoginValidationSet))]

public partial class LoginView : ViewPage{}

Then, you add the following script and methods to your view:

<script type="text/javascript">

 $(function(){

updateSettingsForLoginValidationSet($('#loginForm').validate({rules:{} }));

});</script>

...<form id="loginForm" action="/Authenticate" method="post">

...</form>

<% this.RenderValidationSetScripts(); %>

This all to validate the login HTML form on the client and server-side.

More documentation take a look at the article on CodeProject.com: http://www.codeproject.com/KB/aspnet/MvcValidatorToolkit.aspx

Please see the source code with the included sample site for more examples. You will also find a multi-form example showing how to use the toolkit in conjunction with multiple forms on one HTML page (view).

  评论这张
 
阅读(436)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018