标签:c java a tar ext javascript
要实现默认的验证,需完成如下三步骤:
1.在Web.config里启用客户端验证,在Mvc自带的模版中是默认存在的:
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
两个属性必须都为true时才能启用客户端验证,还有一种替代方法:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
HtmlHelper.ClientValidationEnabled=true;
HtmlHelper.UnobtrusiveJavaScriptEnabled=true;
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
也可以针对单个视图启用或禁用客户端验证:
@model MvcApp.Models.StockContainer
@{
ViewBag.Title="stock";
HtmlHelper.ClientValidationEnabled=false;
}
2.在被验证的View页面上加入这样两个Js代码:
<script src="@Url.Content("~/Script/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Script/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
3.给Model类的各个属性加上对应的验证标记,如:
@Html.EditorFor(model=>model.StockType)
@Html.ValidationMessageFor(model=>model.stockType)
标签:c java a tar ext javascript
原文地址:http://www.cnblogs.com/beesky520/p/3764006.html