3

我刚刚创建了一个新的 MVC 3 项目并使用包管理器更新了所有已安装的包,但是我收到以下错误:

Microsoft JScript 运行时错误:无法设置属性“unobtrusive”的值:对象为空或未定义

以下是相关代码:

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title</title>

        <link href="@Url.Content("~/Content/Reset.css")" rel="stylesheet" type="text/css" />
        <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />

        <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>

        <script src="@Url.Content("~/Scripts/jquery-1.7.2.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/jquery-ui-1.8.20.min.js")" type="text/javascript"></script>

        <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
    </head>

    <body>
        @RenderBody()
    </body>
</html>

来自发生错误的区域的 javascript 片段:

$jQval.unobtrusive = {
        adapters: [],

        parseElement: function (element, skipAttach) {
            /// <summary>
            /// Parses a single HTML element for unobtrusive validation attributes.
            /// </summary>
            /// <param name="element" domElement="true">The HTML element to be parsed.</param>
            /// <param name="skipAttach" type="Boolean">[Optional] true to skip attaching the
            /// validation to the form. If parsing just this single element, you should specify true.
            /// If parsing several elements, you should specify false, and manually attach the validation
            /// to the form when you are finished. The default is false.</param>
            var $element = $(element),
                form = $element.parents("form")[0],
                valInfo, rules, messages;

            if (!form) {  // Cannot do client-side validation without a form
                return;
            }...

有人可以告诉我为什么会发生此错误以及如何解决吗?我只是缺少对所需脚本的引用吗?

谢谢,亚历克斯。

4

2 回答 2

13

你应该包括jquery.validate.js之前jquery.validate.unobtrusive.js

    <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
于 2012-05-25T16:09:46.570 回答
2

您还需要包含 JQuery Validate 脚本

于 2012-05-25T16:07:40.283 回答