[英]How to retrieve success and model error if request is via ajax and display it in modal
I want to know how can I retrieve success and model error and display it in my modal if the post is called via ajax?如果帖子是通过 ajax 调用的,我想知道如何检索成功和 model 错误并将其显示在我的模式中? If success modal must close and if error, a modal should display the custom error and not close modal.
如果成功模式必须关闭并且如果出现错误,则模式应显示自定义错误而不是关闭模式。 So far here is my code:
到目前为止,这是我的代码:
this is called when the user clicks the save button function:当用户点击保存按钮 function 时调用它:
if (result.value) {
var actionUrl = form.attr('action');
var sendData = form.serialize();
$.post(actionUrl, sendData).done(function (data) {
//Error here
//what code to retrieve error here?
//end error
//Success here
swalWithBootstrapButtons.fire(
'Saved!',
'Your data has been saved.',
'success'
)
PlaceHolderElement.find('.modal').modal('hide');
//end success
});
} else if (
/* Read more about handling dismissals below */
result.dismiss === Swal.DismissReason.cancel
) {
}
}
And here is my controller code:这是我的 controller 代码:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(string company, MemberVM member)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = member.Email, Email = member.Email, SiteName = company, Status=true, DateCreated = DateTime.Now, EmailConfirmed = true };
var result = await _userManager.CreateAsync(user, "Password123!");
if (result.Succeeded)
{
}
else
{
foreach (var error in result.Errors)
{
ModelState.AddModelError("", error.Description);
}
return PartialView();
}
}
return PartialView();
}
And I know my validationscripts are working fine, sample screenshot而且我知道我的验证脚本工作正常,示例截图
You can try to return error messages to view rather than returning a PartialView.And put the error messages into somewhere you want.您可以尝试返回错误消息以查看而不是返回 PartialView。并将错误消息放在您想要的位置。
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<Dictionary<string, string>> Create(string company, MemberVM member)
{
Dictionary<string, string> d = new Dictionary<string, string>();
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = member.Email, Email = member.Email, SiteName = company, Status=true, DateCreated = DateTime.Now, EmailConfirmed = true };
var result = await _userManager.CreateAsync(user, "Password123!");
if (result.Succeeded)
{
}
else
{
foreach (var error in result.Errors)
{
d.Add("propertyName", error.Description);
}
return d;
}
}
return d;
}
View Modal:查看模态:
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form method="post">
<div class="form-group">
<label asp-for="FirstName" class="control-label"></label>
<input asp-for="FirstName" class="form-control" />
<span asp-validation-for="FirstName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="LastName" class="control-label"></label>
<input asp-for="LastName" class="form-control" />
<span asp-validation-for="LastName" class="text-danger"></span>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" onclick="CallJs()">Save changes</button>
</div>
</div>
</div>
</div>
js: js:
$.post(actionUrl, sendData).done(function (data) {
//Error here
//what code to retrieve error here?
//end error
if (Object.keys(data).length != 0) {
//if the dictionary is not empty,add error to corresponding span of input
for (const [key, value] of Object.entries(data)) {
$("span[data-valmsg-for=" + key + "]").html(value);
}
} else {
//if the dictionary is empty,close the modal
PlaceHolderElement.find('.modal').modal('hide');
}
//Success here
swalWithBootstrapButtons.fire(
'Saved!',
'Your data has been saved.',
'success'
)
//end success
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.