1

错误:0x800a138f - Microsoft JScript 运行时错误:无法获取属性“unobtrusive”的值:对象为空或未定义

底部列出的 Index.cshtml 是我出现问题的地方。

BundleConfig.cs:

using System.Web;
using System.Web.Optimization;

namespace MyWebMVC
{
   public class BundleConfig
   {
      public static void RegisterBundles(BundleCollection bundles)
      {
          bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                      "~/Scripts/jquery-{version}.js"));

          bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                      "~/Scripts/jquery-ui-{version}.js"));

          bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                      "~/Scripts/jquery.validate*",
                      "~/Scripts/jquery.unobtrusive*"
                      ));

          bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                      "~/Scripts/modernizr-*"));

          bundles.Add(new StyleBundle("~/Content/css").Include(
                      "~/Content/site.css"));

          bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                      "~/Content/themes/base/jquery.ui.core.css",
                      "~/Content/themes/base/jquery.ui.resizable.css",
                      "~/Content/themes/base/jquery.ui.selectable.css",
                      "~/Content/themes/base/jquery.ui.accordion.css",
                      "~/Content/themes/base/jquery.ui.autocomplete.css",
                      "~/Content/themes/base/jquery.ui.button.css",
                      "~/Content/themes/base/jquery.ui.dialog.css",
                      "~/Content/themes/base/jquery.ui.slider.css",
                      "~/Content/themes/base/jquery.ui.tabs.css",
                      "~/Content/themes/base/jquery.ui.datepicker.css",
                      "~/Content/themes/base/jquery.ui.progressbar.css",
                      "~/Content/themes/base/jquery.ui.theme.css"));
      }
   }
}

_rootLayout.cshtml:

<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8" />
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <meta name="viewport" content="width=device-width" />
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")

    <link href="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/Content/css")" rel="stylesheet" type="text/css" />
    <script src="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/Scripts/js")" type="text/javascript"></script>

@Html.DevExpress().GetStyleSheets( 
    new StyleSheet { ExtensionSuite = ExtensionSuite.NavigationAndLayout }, 
    new StyleSheet { ExtensionSuite = ExtensionSuite.Editors }, 
    new StyleSheet { ExtensionSuite = ExtensionSuite.HtmlEditor }, 
    new StyleSheet { ExtensionSuite = ExtensionSuite.GridView }, 
    new StyleSheet { ExtensionSuite = ExtensionSuite.PivotGrid },
    new StyleSheet { ExtensionSuite = ExtensionSuite.Chart },
    new StyleSheet { ExtensionSuite = ExtensionSuite.Report },
    new StyleSheet { ExtensionSuite = ExtensionSuite.Scheduler }
)
@Html.DevExpress().GetScripts( 
    new Script { ExtensionSuite = ExtensionSuite.NavigationAndLayout }, 
    new Script { ExtensionSuite = ExtensionSuite.HtmlEditor }, 
    new Script { ExtensionSuite = ExtensionSuite.GridView }, 
    new Script { ExtensionSuite = ExtensionSuite.PivotGrid },
    new Script { ExtensionSuite = ExtensionSuite.Editors }, 
    new Script { ExtensionSuite = ExtensionSuite.Chart },
    new Script { ExtensionSuite = ExtensionSuite.Report },
    new Script { ExtensionSuite = ExtensionSuite.Scheduler }
)
</head>
<body>
    @Html.DevExpress().Splitter(settings => {
        settings.Name = "MainSplitter";
        settings.AllowResize = false;
        settings.Orientation = System.Web.UI.WebControls.Orientation.Vertical;
        settings.FullscreenMode = true;
        settings.SeparatorVisible = false;
        settings.Styles.Pane.Border.BorderWidth = System.Web.UI.WebControls.Unit.Pixel(0);
        settings.Styles.Pane.Paddings.Padding =    System.Web.UI.WebControls.Unit.Pixel(0);

    settings.Panes.Add(pane => {
        pane.Name = "Header";
        //pane.ScrollBars = System.Web.UI.WebControls.ScrollBars.Auto;
        pane.Size = System.Web.UI.WebControls.Unit.Pixel(140);
        pane.MinSize = System.Web.UI.WebControls.Unit.Pixel(83);
        pane.PaneStyle.BorderBottom.BorderWidth = System.Web.UI.WebControls.Unit.Pixel(1);
        pane.PaneStyle.CssClass = "headerPane";
        pane.SetContent(() => {
            Html.RenderPartial("HeaderPartialView");
        });
    });

    settings.Panes.Add(pane => {
        pane.Name = "Content";
        pane.PaneStyle.CssClass = "mainContentPane";
        pane.MinSize = System.Web.UI.WebControls.Unit.Pixel(375);
        pane.PaneStyle.BackColor = System.Drawing.Color.White;
        pane.PaneStyle.BorderBottom.BorderWidth = System.Web.UI.WebControls.Unit.Pixel(1);
        pane.SetContent(RenderBody().ToHtmlString());
        pane.ScrollBars = System.Web.UI.WebControls.ScrollBars.Auto;
    });

    settings.Panes.Add(pane => {
        pane.Name = "Footer";
        pane.Size = System.Web.UI.WebControls.Unit.Pixel(25);
        pane.PaneStyle.CssClass = "footerPane";
        pane.SetContent(() => {
            Html.RenderPartial("FooterPartialView");
        });
    });

    Scripts.Render("~/bundles/jqueryval");
    Scripts.Render("~/bundles/jquery"); 
}).GetHtml()
</body>
</html>

_mainLayout.cshtml:

@{ Layout = "~/Views/Shared/_rootLayout.cshtml"; }

@Html.DevExpress().Splitter(settings => {
    settings.Name = "ContentSplitter";
    settings.Width = System.Web.UI.WebControls.Unit.Percentage(100);
    settings.Height = System.Web.UI.WebControls.Unit.Percentage(100);
    settings.Styles.Pane.Paddings.Padding = System.Web.UI.WebControls.Unit.Pixel(0);
    settings.Styles.Pane.Border.BorderWidth = System.Web.UI.WebControls.Unit.Pixel(0);
    settings.Panes.Add(subpane => {
        subpane.Name = "ContentCenter";
        subpane.PaneStyle.CssClass = "contentPane";
        subpane.Separator.Visible = DevExpress.Utils.DefaultBoolean.True;
        subpane.ScrollBars = System.Web.UI.WebControls.ScrollBars.Auto;       
        subpane.Separator.SeparatorStyle.Border.BorderWidth = System.Web.UI.WebControls.Unit.Pixel(1);
        subpane.Separator.SeparatorStyle.BorderTop.BorderWidth = System.Web.UI.WebControls.Unit.Pixel(0);
        subpane.SetContent(RenderBody().ToHtmlString());
    });

}).GetHtml()

Index.cshtml:(这是我的脚本出错的地方)

<link rel="stylesheet" type="text/css" href="@Url.Content("~/Content/Docking/CSS/Widgets.css")" />

<script type="text/javascript">
    function displaySuccess(result) {
        popup.Show();
    }

    function PrepareValidationScripts(form) {
        if (!form)
            return;
        if (form.attr("data-executed"))
            return;
        $.validator.unobtrusive.parse(form); // <----- this is where I get the error
        form.attr("data-executed", "true");
    }
</script>

@{
    //this is where I render my partial views
    <snip.....>
    <snip.....>
    <snip.....>
 }
4

1 回答 1

2

在您的布局中,您应该首先包含 jQuery,然后包含 jQueryval:

Scripts.Render("~/bundles/jquery"); 
Scripts.Render("~/bundles/jqueryval");

这是因为 jQuery.validate 插件依赖于 jQuery,并且包含脚本的顺序很重要。

另外,我建议您将所有自定义脚本放在一个专门的部分中,该部分位于其他主要脚本之后:

Scripts.Render("~/bundles/jquery"); 
Scripts.Render("~/bundles/jqueryval");
RenderSection("Scripts", required: false)

然后在您的视图中将所有脚本放在此部分中,以便保留顺序:

@section Scripts {
    <script type="text/javascript">
        function displaySuccess(result) {
            popup.Show();
        }

        function PrepareValidationScripts(form) {
            if (!form)
                return;
            if (form.attr("data-executed"))
                return;
            $.validator.unobtrusive.parse(form);
            form.attr("data-executed", "true");
        }
    </script>
}

@{
    //this is where I render my partial views
    <snip.....>
    <snip.....>
    <snip.....>
 }

当然,您的 CSS 样式也是如此。您应该将它们放在文档中专门的部分中<head>

于 2013-02-07T21:03:07.967 回答