在我的ASP.NET MVC 4.0 Web应用程序中,永远不会调用jQuery验证的validate方法,因此不会进行验证。我想这一定是我遗漏的一些微妙的事情。
生成的html如下......
<html>
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/mvc/4.0/jquery.validate.unobtrusive.min.js"></script>
</head>
<body>
<form id="CreateForm" method="post">
<div class="editor-label">
<label for="Customer_FirstName">FirstName</label>
</div>
<div class="editor-field">
<input class="text-box single-line" id="Customer_FirstName" name="Customer.FirstName" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="Customer.FirstName" data-valmsg-replace="true"></span>
</div>
<p>
<input type="submit" value="Create" />
</p>
</form>
<script>
$(document.ready(function () {
$('#CreateForm').validate({
rules: {
"Customer.FirstName": "required"
},
messages: {
"Customer.FirstName": "Enter first name"
}
});
});
</script>
</body>
</html>