我的代码:
@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]没有方法'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>
它有效!我不知道为什么。谁知道谁?
答案 0 :(得分:0)
尝试为布局页面指定完整的虚拟路径:
@model ColumnVM
@{
ViewBag.Title = "AddColumn3";
Layout = "~/Views/Shared/_layout2.cshtml";
}
就像@Tats_innit所说,它可能是路径解析错误。您也可以使用布局的名称(如果它是唯一的),因为MVC最终会在Shared文件夹中查找非限定视图/布局。