[英]Add an error in ValidationSummary programmatically in data-ajax-success call
I'm displaying, with success, a ValidationSummary
containing the errors that the jQuery
unobstrusive validator detects : 我成功地展示了一个包含jQuery
unobstrusive验证器检测到的错误的ValidationSummary
:
<form asp-controller="PlanningYearsEditModal" asp-action="EditPlanningYear" data-ajax="true" data-ajax-method="POST" data-ajax-success="onSuccess">
<div id="content" class="modal-body">
@Html.ValidationSummary(false, null, new { @class = "text-danger" })
<table class="inner" style="width:100%" border=1>
<tr class="azurbox rounded3 white bold">
<td style="width:20%">@CommonResources.CommonField</td>
<td style="width:80%">@CommonResources.CommonValue</td>
</tr>
<tr class="underline">
<td>@Html.LabelFor(model => model.LabelFr)</td>
<td>
@Html.EditorFor(model => model.LabelFr)
@Html.ValidationMessageFor(model => model.LabelFr, "*", new { @class = "text-danger" })
</td>
</tr>
<tr class="underline">
<td>@Html.LabelFor(model => model.LabelNl)</td>
<td>
@Html.EditorFor(model => model.LabelNl)
@Html.ValidationMessageFor(model => model.LabelNl, "*", new { @class = "text-danger" })
</td>
</tr>
</table>
</div>
<div class="modal-footer">
@Html.HiddenFor(model => model.Id)
<button type="button" class="btn" data-dismiss="modal">Close</button>
<button type="submit" class="btn">Save changes</button>
<div id="Results"></div>
</div>
</form>
After these simple checks, I have to do manual integrity checks in my controller, who returns a JSON
containing the errors. 在这些简单的检查之后,我必须在我的控制器中进行手动完整性检查,该控制器返回包含错误的JSON
。 I would like to use the validator to be able to display these errors in the ValidationSummary. 我想使用验证器能够在ValidationSummary中显示这些错误。 I could probably update the html code with jQuery
manually, but it causes a lot of issues (sometimes the validation summary already exists, sometimes not, sometimes you just have to replace a few bullets, sometimes not, ...). 我可以手动用jQuery
更新html代码,但它会导致很多问题(有时候验证摘要已经存在,有时候不会,有时你只需要更换一些子弹,有时候不需要......)。
I feel like there is probably a way to do this in a clean way. 我觉得有可能以一种干净的方式做到这一点。
I tried something like this, but it doesn't display anything on screen (and I can confirm that is code is being called) : 我试过这样的东西,但它没有在屏幕上显示任何内容(我可以确认是在调用代码):
var onSuccess = function (errors) {
var errorArray = {};
errorArray["LabelFr"] = 'Some error thrown by the controller';
$('#myForm').validate().showErrors(errorArray);
};
What can I do ? 我能做什么 ?
When you use mvc's client side validatiion, the jquery.validate.unobtrusive.js
plug-in parses the document and configures the $.validator
in jquery.validate.js
. 当您使用mvc的客户端验证时, jquery.validate.unobtrusive.js
插件解析文档并在jquery.validate.js
配置$.validator
jquery.validate.js
。 You should not attempt to modify the $.validator
or call validate()
. 您不应尝试修改$.validator
或调用validate()
。
Your @Html.ValidationSummary()
generates a <div>
(with a [data-valmsg-summary]
attribute) which contains a <ul>
element. 您的@Html.ValidationSummary()
生成一个<div>
(带有[data-valmsg-summary]
属性),其中包含<ul>
元素。 To add the messages, you can simply add <li>
elements containing the error message 要添加消息,只需添加包含错误消息的<li>
元素即可
var vs = $('[data-valmsg-summary]'); // get the div
if (errorArray.length > 0) {
// update class name
vs.addClass('validation-summary-errors').removeClass('validation-summary-valid');
}
$.each(errorArray, function(index, item) {
// add each error
vs.children('ul').append($('<li></li>').text(item));
});
If this is being called multiple times, and you do not want to display previously added errors, you can give the <li>
elements a class name, say 如果多次调用它,并且您不想显示以前添加的错误,则可以为<li>
元素指定一个类名,比如说
vs.children('ul').append($('<li class="myerror"></li>').text(item));
and then remove them using vs.find('.myerror').remove();
然后使用vs.find('.myerror').remove();
删除它们vs.find('.myerror').remove();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.