3

I have just created a new MVC 3 project and updated all the installed packages using the package manager, however I am recieving the following error:

Microsoft JScript runtime error: Unable to set value of the property 'unobtrusive': object is null or undefined

Here is relevant code:

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

A javascript snippet from the area the error occurs:

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

Can someone please tell me why this error is occurring and how I can fix it? Am I simply missing a reference to a required script?

Thanks, Alex.

2 Answers 2

13

You should include jquery.validate.js before 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
  • 1
    Thank you @archil So in MVC4 when you face this problem you should reorder the bundle statement to: bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include( "~/Scripts/jquery.validate*", "~/Scripts/jquery.validate.unobtrusive.js", "~/Scripts/jquery.unobtrusive*"));
    – Shadi
    May 21, 2013 at 5:42
2

You need to also include the JQuery Validate script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.