jquery.validate.unobtrusive.bootstrap icon indicating copy to clipboard operation
jquery.validate.unobtrusive.bootstrap copied to clipboard

Script order in ASP.NET MVC 5 template jqueryval bundle

Open older opened this issue 11 years ago • 2 comments

In the default MVC 5 template for web application jqueryval bundle looks like this:

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

When nuget package jquery.validate.unobtrusive.bootstrap is installed it results in jquery.validate.unobtrusive.bootstrap included in this bundle. But bundle includes scripts in alphabetical order which results in jquery.validate.unobtrusive.bootstrap.js included before jquery.validate.unobtrusive.js and not working because of that. I'm solving it currently by using this code in BundleConfig.cs:

Bundle jQueryValidateBundle = new ScriptBundle("~/bundles/jqueryval")
    .Include("~/Scripts/jquery.validate*");
jQueryValidateBundle.Orderer = new JQueryValidateBundleOrderer();
bundles.Add(jQueryValidateBundle);

where JQueryValidateBundleOrderer class looks like this:

private class JQueryValidateBundleOrderer : IBundleOrderer
{
    public IEnumerable<BundleFile> OrderFiles(BundleContext context, IEnumerable<BundleFile> files)
    {
        // Sort files by number of dots in file name
        return files.OrderBy(file => file.VirtualFile.Name.Count(ch => ch == '.'));
    }
}

older avatar Oct 16 '14 12:10 older

You could also get around that by using more specific paths:

"~/scripts/jquery.validate.min.js",
"~/scripts/jquery.validate.unobtrusive.min.js",
"~/scripts/jquery.validate.unobtrusive.bootstrap.js"

If you aren't planning on tweaking the plugin files, there really isn't a point to referencing the un-minified versions, even in debug mode.

Obviously, that's purely my opinion. I can see your solution having a broader use in general.

tiesont avatar Oct 17 '14 08:10 tiesont

@tiesont Problem here is that if you use default MVC template to create new project and reference nuget package for jquery.validate.unobtrusive.bootstrap - script gets included in bundle but it doesn't work. It is not a big problem if you never include this bundle in your views, other than it takes some place in BundleCollection.

older avatar Oct 17 '14 09:10 older