在回答这个问题(MVC 5 - 无法运行全球化)时,我用一堆“<sript src="..." 声明和一些 js 解决了这个问题。
我没有管理的内容:我想捆绑脚本。如果我这样尝试(摘自 bundleConfig.cs):
bundles.Add(new ScriptBundle("~/bundles/jqueryvalDe").Include(
"~/Scripts/jquery.validate.js",
"~/Scripts/jquery.validate.unobtrusive.js",
"~/Scripts/cldr.js",
"~/Scripts/cldr/event.js",
"~/Scripts/cldr/supplemental.js",
"~/Scripts/cldr/unresolved.js",
"~/Scripts/globalize.js",
"~/Scripts/globalize/currency.js",
"~/Scripts/globalize/number.js",
"~/Scripts/globalize/date.js",
"~/Scripts/globalize/plural.js",
"~/Scripts/globalize/relative-time.js",
"~/Scripts/globalize/unit.js",
"~/Scripts/jquery.validate.globalize.js"
));
但我从 JavaScript 中得到一个错误:“Globalize”未定义
看来,脚本的顺序改变了......
目前我使用一种解决方法:我编写了一个自定义 HTM-Helper。
public static class GermanDateValidationExtension
{
public static MvcHtmlString ScriptsForGermanDateValidation(this HtmlHelper helper)
{
return new MvcHtmlString("<script src=\"~/ Scripts / jquery.validate.js\"></script>\n" +
"<script src = \"~/Scripts/jquery.validate.unobtrusive.js\"></script>\n" +
"<script src = \"~/Scripts/cldr.js\"></script>\n" +
"<script src = \"~/Scripts/cldr/event.js\"></ script>\n" +
"<script src = \"~/Scripts/cldr/supplemental.js\"></script>\n" +
"<script src = \"~/Scripts/cldr/unresolved.js\"></script>\n" +
"<script src = \"~/Scripts/globalize.js\"></script>\n" +
"<script src = \"~/Scripts/globalize/currency.js\" ></script>\n" +
"<script src = \"~/Scripts/globalize/number.js\"></script>\n" +
"<script src = \"~/Scripts/globalize/date.js\"></script>\n" +
"<script src = \"~/Scripts/globalize/plural.js\" ></script>\n" +
"<script src = \"~/Scripts/globalize/relative-time.js\"></script>\n" +
"<script src = \"~/Scripts/globalize/unit.js\"></script>\n" +
"<script src = \"~/Scripts/jquery.validate.globalize.js\"></script>\n" +
"<script>\n" +
"$.when(\n" +
"$.get(\"/Scripts/cldr/main/de/ca-gregorian.json\"),\n" +
"$.get(\"/Scripts/cldr/main/de/numbers.json\"),\n" +
"$.get(\"/Scripts/cldr/supplemental/likelySubtags.json\"),\n" +
"$.get(\"/Scripts/cldr/supplemental/timeData.json\"),\n" +
"$.get(\"/Scripts/cldr/supplemental/weekData.json\")\n" +
").then(function() {\n" +
"return [].slice.apply(arguments, [0]).map(function(result) {\n" +
"return result[0];\n" +
"});\n" +
"}).then(Globalize.load)\n" +
".then(function() {\n" +
"Globalize.locale(\"de-DE\");\n" +
"});\n" +
"</ script > ");
}
}
并在视图中使用它:
...
@section Scripts {
@Html.ScriptsForGermanDateValidation()
}
...
我的问题是:如何正确捆绑脚本?