我的代码:
@model ColumnVM
@{
ViewBag.Title = "AddColumn3";
Layout = "../Shared/_layout2.cshtml";
}
<script type="text/javascript">
$(function () {
$("form").validate().settings.submitHandler = function (form) {
alert('valid form');
form.submit();
};
});
</script>
@using (Html.BeginForm("addcolumn3", "Course", FormMethod.Post))
{
<fieldset>
<legend>ColumnVM</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
在 _Layout2.cshtml
<!DOCTYPE html>
<html>
<head>
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Layout.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
</head>
<body>
@RenderBody()
</body>
</html>
现在浏览器显示错误:Uncaught TypeError: Object [object Object] has no method 'validate'。如果我更改 Layout=null,并在代码中手动添加
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
它有效!我不知道为什么。有人知道吗?