我的代码中有这个验证摘要:
@Html.ValidationSummary(false, "Fix Error", new { id = "valSumId" } )
是否有 API 可以在运行时添加验证错误?虽然下面的代码有效,但感觉很脆弱,没有什么可以阻止 ASP.NET MVC 团队在下一个 ASP.NET MVC 版本中不使用 ul li 标签来显示错误。
$('input[type=submit]').click(function (e) {
e.preventDefault();
if ($('form').valid()) {
/* ajax submit here */
if (anErrorOccured) {
$('#valSumId').removeClass().addClass('validation-summary-errors');
errorList = $('#valSumId > ul');
// the message is variable, coming from JSON, and can be a list
errorList.append($('<li />').text('Your ordered quantity is above the stock level'));
}
}
});