新鲜的MVC 3项目给了我奇怪的不显眼的验证脚本错误

时间:2012-05-25 16:05:55

标签: c# javascript asp.net-mvc-3 .net-4.0 unobtrusive-validation

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

  

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

以下是相关代码:

<!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;
            }...

有人可以告诉我为什么会出现这个错误以及如何修复它?我只是错过了对所需脚本的引用吗?

谢谢, 亚历克斯。

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:2)

您还需要包含JQuery Validate脚本