Bundled scripts not working MVC

五迷三道 提交于 2019-12-11 02:39:38

问题


I have bundled jquery Validate scripts like :

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

and am rendering in the _Layout as

@Scripts.Render("~/bundles/jqueryval")

But when i try to Validate my form on button click like :

$("form").validate({
                    errorPlacement: function (error, element) {
                        error.insertAfter(element).css("display", "inline");
                    }
                });

Its giving me the error like :

0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'validate'

So anyone suggest me for this problem.


回答1:


The runtime deferentiating minified versions from un-minified(based on standard JavaScript naming conventions), so if you specify "~/Scripts/jquery.unobtrusive*" path the specifier will include jquery.unobtrusive.js when minification turned off and jquery.unobtrusive.min.js when minification turned on.

Check out if you have jquery.unobtrusive.min.js and jquery.validate.min.js files in your Scripts folder.

To turn on minification manually - paste BundleTable.EnableOptimizations = true; after adding your bundles.

EDIT:

Your error saying that validate method didn't declared yet. Please check out if your @Scripts.Render("~/bundles/jqueryval") is located before you call $("form").validate() or wrap calling of validate into jQuery(callback) function like this:

$(function(){
    $("form").validate({
                    errorPlacement: function (error, element) {
                        error.insertAfter(element).css("display", "inline");
                    }
                });
})

That's will wait until all document is loaded.




回答2:


Could you make sure UnobtrusiveJavaScriptEnabled is enabled in web.config?

  <appSettings>
    ...
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
    ...
  </appSettings>


来源:https://stackoverflow.com/questions/17456360/bundled-scripts-not-working-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!