错误:0x800a138f - Microsoft JScript运行时错误:无法获取属性'unobtrusive'的值:object为null或undefined
底部列出的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.....>
}
答案 0 :(得分: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>
中的专用部分。