【发布时间】:2021-05-23 11:16:42
【问题描述】:
我是在 asp.net 核心中使用甜蜜警报的新手。在这里,当我想在添加记录后提交一个将数据存储在数据库中的表单时,我想显示一个带有甜蜜警报的确认警报。这是我的控制器代码:
public async Task<IActionResult> RequestJob()
{
ViewBag.Result = false;
return View();
}
[HttpPost]
public async Task<IActionResult> RequestJob(ResumeViewModel viewModel)
{
if (ModelState.IsValid)
{
Resume resume = new Resume()
{
Name = viewModel.Name,
LastName = viewModel.LastName,
Age = viewModel.Age,
Mobile = viewModel.Mobile,
Mail = viewModel.Mail,
};
await _temp.AddResume(resume);
ViewBag.Result = true;
ModelState.Clear();
}
}
ViewBag.Result = false;
return View(viewModel);
}
这是我的 ViewBag Condition 中的 Sweet alert 代码:
@if (ViewBag.Result)
{
@section mySection{
<script>
$("#submition").on('click', (function () {
swal({
title: "Message",
text: "Your message has been sent successfully",
type: 'success',
showCancelButton: false,
allowOutsideClick: false,
confirmButtonColor: "green",
confirmButtonText: "Ok"
}).then(func1tion () {
window.location.reload(true);
});
}));
</script>
}}
但是当我提交表单时,虽然表单提交了正确的信息,但甜蜜的警报不起作用。请完善我的代码,以便甜蜜警报工作,我不知道为什么甜蜜警报在我的 ViewBag 条件下不起作用?!
【问题讨论】:
-
您的控制器代码看起来无效(
if (ModelState.IsValid)右括号加倍。但即使您修复了这个问题,也有一个return。这意味着ViewBag.Result = false;将始终覆盖。如果没有给你一个想法,请更新问题。
标签: c# asp.net-core asp.net-core-3.1 sweetalert2 confirmation