我想要一个真正简单的模态对话框运行.所以按照一个教程,我结束了这段代码:
BundleConfig:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include( "~/Scripts/jquery-1.8.2.min.js")); bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include( "~/Scripts/jquery-ui-1.8.24.js"));
_布局:
<head> <Meta charset="utf-8" /> <title>@ViewBag.Title - My ASP.NET MVC Application</title> <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/jquery") @Scripts.Render("~/bundles/jqueryui") </head> <body> <div id="body"> @RenderSection("featured",required: false) <section class="content-wrapper main-content clear-fix"> @RenderBody() </section> </div> <footer> <div class="content-wrapper"> <div class="float-left"> <p>© @DateTime.Now.Year - My ASP.NET MVC Application</p> </div> </div> </footer> @RenderSection("scripts",required: false) </body>
和索引:
@section featured { <section class="featured"> <div class="content-wrapper"> <hgroup class="title"> <h1>@ViewBag.Title.</h1> <h2>@ViewBag.Message</h2> </hgroup> <script type="text/javascript"> $( "#dialog-form" ).dialog({ autoOpen: false,height: 300,width: 350,modal: true,buttons: { Cancel: function() { $( this ).dialog( "close" ); } },close: function() { allFields.val( "" ).removeClass( "ui-state-error" ); } }); $( "#create-user" ) .button() .click(function() { $( "#dialog-form" ).dialog( "open" ); }); </script> <div style="float: left; width: 250px;"> <button id="create-user">Create new user</button> </div> </div> </section> }
但是当我运行它时,最终会出现0x800a1391 – JavaScript运行时错误:jquery-ui库中的’jQuery’未定义.如果我只是将代码放在一个html页面,它的工作原理如预期.所以问题来自于MVC项目.我在Windows 8上使用visual studio 2012.任何想法?
解决方法
默认情况下,MVC bundler会忽略文件名中的.min文件.
使用未经精简的jQuery版本可以解决问题(或者只是重命名文件) – 在部署时,bundler将尽可能地缩小jQuery文件.
更新
您可以通过清除RegisterBundles方法中的IgnoreList来更改此行为(但我建议使用默认值,只需重命名文件):
// Clear all items from the ignore list to allow minified CSS and JavaScript // files in debug mode bundles.IgnoreList.Clear(); // Add back the default ignore list rules sans the ones which affect minified // files and debug mode bundles.IgnoreList.Ignore("*.intellisense.js"); bundles.IgnoreList.Ignore("*-vsdoc.js"); bundles.IgnoreList.Ignore("*.debug.js",OptimizationMode.WhenEnabled);
查看更多在Telerik docs.