In some context, the DOM hierarchy:
Layout.cshtml
> View
> Partial View
Layout file contains:
<head>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")
</head>
<body>
<div>
@RenderBody()
</div>
@RenderSection("scripts", required: false)
</body>
The view contains a form. After submitting the form, the AJAX call returns a partial view, which is inserted into the view using $ ('selector'). Html (PartialViewResult).
Partial view contains:
// @Scripts.Render("~/bundles/jquery") // [†]
@using(Ajax.BeginForm(...)
{
// MinRate has the appropriate "lessthanproperty" data-val HTML properties
@Html.EditorFor(x => x.MinRate)
// MaxRate has the appropriate "greaterthanproperty" data-val HTML properties
@Html.EditorFor(x => x.MaxRate)
@Html.ValidationSummary()
<button type="submit">Submit</button>
}
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/MapRates")
<script>
$(document).ready(function () {
console.log("CSHTML, ON READY");
});
(function() {
console.log("CSHTML, IIFE");
$.validator.addMethod("lessthanproperty", function (value, element, params) {
return Number(value) < Number($(params).val());
});
$.validator.addMethod("greaterthanproperty", function (value, element, params) {
return Number(value) > Number($(params).val());
});
})();
</script>
MapRates Javascript file contains:
$(document).ready(function () {
console.log("JS, ON READY");
});
(function () {
console.log("JS, IIFE")
$.validator.unobtrusive.adapters.add("lessthanproperty", ["comparisonpropertyname"], function (options) {
options.rules["lessthanproperty"] = "#" + options.params.comparisonpropertyname;
options.messages["lessthanproperty"] = options.message;
});
$.validator.unobtrusive.adapters.add("greaterthanproperty", ["comparisonpropertyname"], function (options) {
options.rules["greaterthanproperty"] = "#" + options.params.comparisonpropertyname;
options.messages["greaterthanproperty"] = options.message;
});
})();
... , , . MinRate MaxRate, ValidationSummary . † , jQuery, @Scripts.Render("~/bundles/jquery") , Ajax.BeginForm.
jquery script Layout, , , , , , . , jquery .
, jquery, :
JS, IIFE
CSHTML, IIFE
JS, ON READY
CSHTML, ON READY
, jquery, :
JS, ON READY
JS, IIFE
CSHTML, ON READY
CSHTML, IIFE
jquery script . , , , -, js .
? , - , jquery ? !
edit: , , Partial View . , , , .