@model OA.Models.SysLogModel
@{
ViewBag.Title = "Index";
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<script src="@Url.Content("~/Script/jquery.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Script/jquery.validate.min.js")" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#CreateForm").validate({
rules: {
Operator: {
required: true,
minlength: 5
},
Message: {
required: true,
maxlength: 4
}
},
messages: {
Operator: {
required: "请输入标题",
minlength: "长度不能小于5"
},
Message: {
required: "请输入年龄",
maxlength: "个数小于4"
}
}
})
</script>
</head>
<body>
<h2>Index</h2>
@using (Html.BeginForm("Login", "Account", null, FormMethod.Post, new { Id = "CreateForm" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>SysLogModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Operator)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Operator)
@Html.ValidationMessageFor(model => model.Operator)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Message)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Message)
@Html.ValidationMessageFor(model => model.Message)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
</body>
</html>
代码有点乱,我删除了一些 我点击提交但是不会进行验证,我是哪里错了
还是方法不对
第一次使用
你这么处理有点多余了,因为MVC 已经把验证集成了不需要用户手动去写了用法如下
1 给ViewModel属性加上必填特性
[DisplayName("PartyA")] [Required(ErrorMessage = "PartyA Required")] public string PartyA { get; set; }
2 引入Script 文件夹下的两个js文件及jquery.ui的css文件
<link href="~/Content/jquery.ui/jquery-ui.css" rel="stylesheet" />
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
3 view的写法就与你的是一样的了
谢谢你的回答
还有一个问题是:
验证失败后,还是会提交
@生活还是要继续: 那就一定是写法有问题,正常情况下,验证没通过是没法提交的
有QQ吗
弄了好久了,不知道哪里错了
同时也注意下插件版本,最新的不支持在IE低版本上验证了。