無法讓jQuery Validate插件發揮出色。爲什麼<form>被賦予NoValidate屬性?
型號
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
由於每[插件的頁面(http://bassistance.de/jquery-plugins/jquery-plugin-validation/),版本1.10已經只通過jQuery 1.8進行了測試。 – Sparky