问题
I want to add globalization because the site asks the user for a date. And my german user want to type "31.12.1966" and not "1966-12-31".
So I add the nuget-Packages "jQuery.Validation.Globalize" and "jquery-globalize" to the project.
Now I am not able to configure my BundleConfig! From my research I know, that I Need globalize.js and some other files. So I try to make a bündle:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.validate*"));
bundles.Add(new ScriptBundle("~/bundles/global").Include(
"~/Scripts/globalize.js",
"~/Scripts/cldr.js").IncludeDirectory("~/Scripts/cldr/",
"~/Scripts/globalize/")
);
The using in the view:
...
@section Scripts {
@Scripts.Render("~/bundles/global")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/unobtrusiveajax")
<script type="text/javascript">
$(function () {
$.validator.methods.date = function (value, element) {
Globalize.culture("de-DE");
// you can alternatively pass the culture to parseDate instead of
// setting the culture above, like so:
// parseDate(value, null, "en-AU")
return this.optional(element) || Globalize.parseDate(value) !== null;
}
});
</script>
}
But I get an error:
Error at line 9, column 5 in http://localhost:58289/Scripts/jquery.validate.globalize.js
0x800a138f - runtimeerror in JavaScript:
The property "methods" of a undefindes or null-pointer can not bei called
I translated the message from this german original:
Ausnahmefehler in Zeile 9, Spalte 5 in http://localhost:58289/Scripts/jquery.validate.globalize.js
0x800a138f - Laufzeitfehler in JavaScript:
Die Eigenschaft "methods" eines undefinierten oder Nullverweises kann nicht abgerufen werden.
Need I more/other files in the bundle?
What can I do? Any help?
Sincerly Peter
回答1:
I solved it this way:
In my view is the following script block:
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="~/Scripts/cldr.js"></script>
<script src="~/Scripts/cldr/event.js"></script>
<script src="~/Scripts/cldr/supplemental.js"></script>
<script src="~/Scripts/cldr/unresolved.js"></script>
<script src="~/Scripts/globalize.js"></script>
<script src="~/Scripts/globalize/currency.js"></script>
<script src="~/Scripts/globalize/number.js"></script>
<script src="~/Scripts/globalize/date.js"></script>
<script src="~/Scripts/globalize/plural.js"></script>
<script src="~/Scripts/globalize/relative-time.js"></script>
<script src="~/Scripts/globalize/unit.js"></script>
<script src="~/Scripts/jquery.validate.globalize.js"></script>
<script>
$(document).ready(function () {
// Use $.getJSON instead of $.get if your server is not configured to return the
// right MIME type for .json files.
$.when(
$.get("/Scripts/cldr/main/de/ca-gregorian.json"),
$.get("/Scripts/cldr/main/de/numbers.json"),
$.get("/Scripts/cldr/supplemental/likelySubtags.json"),
$.get("/Scripts/cldr/supplemental/timeData.json"),
$.get("/Scripts/cldr/supplemental/weekData.json")
).then(function () {
// Normalize $.get results, we only need the JSON, not the request statuses.
return [].slice.apply(arguments, [0]).map(function (result) {
return result[0];
});
}).then(Globalize.load)
.then(function () {
Globalize.locale("de-DE");
});
});
My data-class has an annotiation like this:
...
[Required]
[DataType(DataType.DateTime)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy}")]
public DateTime Geburtsdatum { get; set; }
...
And - very important!! - you must get the data-files! This is nessecary because the clrd-data is not part of the NuGet-Packages "jQuery.Validation.Globalize" / "jquery-globalize". (Yes, it is mentioned on the Project-page - but I didn't see it... :-( )
I installed Bower (per NuGet) and installed then via Bower the Cldr-data. Example:
bower install cldr-dates-full
(see overview over packages and install instruction 1)
Then I moved the needed json-files (here you find an online-tool for file-selection 2) from Directory "bower_components" to "scripts\cldr\main\de" respectively "scripts\cldr\supplemental".
I add them to the project and mark them as "Content", "no copy".
So finally it works!!! :-)
If I manage it to bundle the js- and json-files, I will update the answer.
来源:https://stackoverflow.com/questions/44763260/mvc-5-can-not-get-globalisation-running