默认是启用的,也可以在配置文件里设置启用和关闭这项功能;
<configuration>
<appSettings>
<add key="ClientValidationEnabled" value="true" /><add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
</configuration>
进行客户端验证,需要在视图文件里引用jQuery验证插件:
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"
type="text/javascript"?</script>
<script
src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"
type="text/javascript"?</script>
<h2>Create Auction</h2>
@using(Html.BeginForm())
{
@Html.ValidationSummary()<p>
@Html.LabelFor(model=>model.Title)@Html.EditorFor(model=>model.Title)@Html.ValidationMessageFor(model=>model.Title,"*")</p>
}
public class Auction
{
[Required][StringLength(50,ErrorMessage="Title cannot be longer than 50 characters")]public string Title{get;set;}[Range(1,10000,ErrorMessage="The auction‘s starting price must be at least 1")]public decimal StartPrice{get;set}
}