For those who stumble upon this, who do not want to limit themselves to moving @Html.ValidationSummary into a form, that is, he lives in _Layout.cshtml, and you like it there, here's the way around it.
This method is apparently used by Microsoft to populate @Html.ValidationSummary . It has a standard form, it looks for data-valmsg-summary-true in $('this') . this in this case is the calling form. Well, my @Html.ValidationSummary lives in pageBody <div> on _Layout.cshtml so that it is DRY.
function onErrors(event, validator) { // '#pageBody' is the containing element var container = $('#pageBody').find("[data-valmsg-summary=true]"), list = container.find("ul"); if (list && list.length && validator.errorList.length) { list.empty(); container.addClass("validation-summary-errors").removeClass("validation-summary-valid"); $.each(validator.errorList, function () { $("<li />").html(this.message).appendTo(list); }); } }
So far, I have only changed this:
var container = $('this').find("[data-valmsg-summary=true]")
:
var container = $('#pageBody').find("[data-valmsg-summary=true]")
Now I start the test by clicking the button. To run onErrors(event, validator) , I used the following jQuery:
$('#btnSave').click(function () { if ($('form').valid()) { // Do stuff } else { onErrors(event, $('form').data('validator')); } });
Voila, @ Html.ValidationSummary is populated even when using jQuery.unobtrusive.
Many thanks to Leniel Macaferi for pointing me in the right direction: http://www.leniel.net/2013/08/customizing-aspnet-mvc-html-validation-summary-with-bootstrap-3-panel.html # sthash.jGRguVuV.qSjYUlhS.dpbs