xVal MVC验证框架的使用

本文介绍了xVal框架在ASP.NET MVC项目中的使用方法,包括如何安装配置xVal及进行客户端验证,并通过示例展示了如何利用元数据类来进行模型验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 参考资料

xVal - a validation framework for ASP.NET MVC

xVal源代码下载地址:http://xval.codeplex.com/

注意,目前的xVal支持的Jquery框架式1.2.6的版本,高版本的反而不支持。

2. 使用步骤

1. 引用xVal程序集(注意,xVal 有源代码发布,但是绑定的是发布时的MVC程序集,可以把MVC的应用换成当前的程序集,重新编译即可);

2. 把相关的 js文件拷贝到Script文件夹中;

3. 想web.config文件中增加xVal.Html命名空间的引用;

<namespaces>

<!-- leave rest as-is -->

<add namespace="xVal.Html"/>

</namespaces>

4. 如果你的页面要使用xVal的客户端验证,最好的实践是把validation的Js文件引用到MasterPage中,这样,View使用该MasterPage后,就自动的引用这些Js了。

<head>

    <script type="text/javascript" src="<%= ResolveUrl("~/Scripts/jquery-1.2.6.js")%>"></script>

    <script type="text/javascript" src="<%= ResolveUrl("~/Scripts/jquery.validate.js")%>"></script>

    <script type="text/javascript" src="<%= ResolveUrl("~/Scripts/xVal.jquery.validate.js")%>"></script>

</head>

5.  相关的js在源代码包里面可以找到。

3. Buddy Class协助验证

闲话少说,看代码。

4. [MetadataType(typeof(Tenant.MetaData))]

5. public partial class Tenant

6.     {

7. private sealed class MetaData

8.         {

9.             [Required(ErrorMessage="英文名称必填")]

10.             [StringLength(30,ErrorMessage="英文名称的长度不能超过30个字符。")]

11.             [RegularExpression("{a-z}",ErrorMessage="请输入英文字符")]

12. public string Name

13.             {

14. get;

15. set;

16.             }

17.

18.             [Required(ErrorMessage="名称必填")]

19.             [StringLength(30)]

20. public string Alias

21.             {

22. get;

23. set;

24.             }

25.

26.

27.         }

28.     }

转载于:https://www.cnblogs.com/hearticy/archive/2009/12/01/1614855.html

This function mmasub performs one MMA-iteration, aimed at % solving the nonlinear programming problem: % % Minimize f_0(x) + a_0*z + sum( c_i*y_i + 0.5*d_i*(y_i)^2 ) % subject to f_i(x) - a_i*z - y_i <= 0, i = 1,...,m % xmin_j <= x_j = 0, y_i >= 0, i = 1,...,m %*** INPUT: % % m = The number of general constraints. % n = The number of variables x_j. % iter = Current iteration number ( =1 the first time mmasub is called). % xval = Column vector with the current values of the variables x_j. % xmin = Column vector with the lower bounds for the variables x_j. % xmax = Column vector with the upper bounds for the variables x_j. % xold1 = xval, one iteration ago (provided that iter>1). % xold2 = xval, two iterations ago (provided that iter>2). % f0val = The value of the objective function f_0 at xval. % df0dx = Column vector with the derivatives of the objective function % f_0 with respect to the variables x_j, calculated at xval. % df0dx2 = Column vector with the non-mixed second derivatives of the % objective function f_0 with respect to the variables x_j, % calculated at xval. df0dx2(j) = the second derivative % of f_0 with respect to x_j (twice). % Important note: If second derivatives are not available, % simply let df0dx2 = 0*df0dx. % fval = Column vector with the values of the constraint functions f_i, % calculated at xval. % dfdx = (m x n)-matrix with the derivatives of the constraint functions % f_i with respect to the variables x_j, calculated at xval. % dfdx(i,j) = the derivative of f_i with respect to x_j. % dfdx2 = (m x n)-matrix with the non-mixed second derivatives of the % constraint functions f_i with respect to the variables x_j, % calculated at xval. dfdx2(i,j) = the second derivative % of f_i with respect to x_j (twice). %
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值