-1

我的代码:

@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>

它有效!我不知道为什么。有人知道吗?

4

1 回答 1

0

尝试为您的布局页面指定完整的虚拟路径:

@model ColumnVM
@{
    ViewBag.Title = "AddColumn3";
    Layout = "~/Views/Shared/_layout2.cshtml";
}

就像@Tats_innit 所说,这可能是路径解析错误。如果它是唯一的,您也可以只使用布局的名称,因为 MVC 最终会在 Shared 文件夹中查找不合格的视图/布局。

于 2013-04-21T01:59:43.460 回答