有麻烦获取jQuery验证插件播放不错。
模型
public class FooVM { [required] public string Name { get; set; } }
布局
<!DOCTYPE html> <html> <head> <Meta charset="utf-8" /> <script src="@Url.Content("~/scripts/jquery-1.9.0.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/scripts/jquery-migrate-1.0.0.min.js")" type="text/javascript"></script> <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> <script src="@Url.Content("~/scripts/bootstrap.min.js")" type="text/javascript"></script> <link href="@Url.Content("~/content/bootstrap-responsive.min.css")" rel="stylesheet" type="text/css" /> <link href="@Url.Content("~/content/bootstrap.min.css")" rel="stylesheet" type="text/css" /> <title>@ViewBag.Title</title> </head> <body> <div class="container"> <div class="row"> <div class="span12"> <div class="navbar"> <div class="navbar-inner"> <a class="brand" href="#">idoneit</a> <ul class="nav"> <li class="menu-link">@Html.ActionLink("Home","index","bar")</li> </ul> </div> </div> </div> <div class="span12 error-container"> </div> <div class="span12 main-body"> @RenderBody() </div> </div> </div> </body> </html>
视图
model bootstrapvalidate.Models.FooVM @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } @using (Html.BeginForm("add","bar",FormMethod.Post,new { @class = "form-horizontal" })) { <fieldset> @Html.ValidationSummary() <legend>Testing Bootstrap & Validate</legend> <div class="control-group"> <label for="Name" class="control-label">Name</label> <div class="controls"> @Html.TextBoxFor(x => x.Name) </div> <button type="submit" class="btn">Add!</button> </div> </fieldset> }
有一件我注意到我以前没看过的是标记包含“novalidate”属性
<form action="/bar/add" class="form-horizontal" method="post" novalidate="novalidate">
从我读过的位,这是阻止验证的HTML5属性。那么是的,这是我假设的原因。
问题是 – 为什么bejesus被添加到表单?我没有要求
编辑:做了一些挖掘基于@rob的答案,似乎jquery验证是抛出一个异常,因此回发..
这是jquery.validate 1.10
解决方法
novalidate
属性由jquery.validate第32行添加:
// Add novalidate tag if HTML5. this.attr('novalidate','novalidate');
如果你使用HTML5,then remove the attribute:
$("#contactForm").validate(); $("#contactForm").removeAttr("novalidate");
在你的web.config中,确保你有:
<appSettings> ... <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings>
还要确保它们没有注释掉。