【问题标题】:Sweet alert confirmation dialog in asp.net coreasp.net 核心中的甜蜜警报确认对话框
【发布时间】: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


【解决方案1】:

你的代码有两个问题。

@kordiseps 提到了第一个。当 ModelState.IsVaild 为 true 时,您没有返回 View,因此 ViewBag 的值始终为 false,并且该部分将永远不会被渲染。

第二个问题:sweet alert 的工作依赖于 ViewBag 条件,所以这里你不应该在表单提交事件中这样做。相反,您需要在页面加载函数中执行此操作。

如下更改您的代码:

控制器:

[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();
        return View(viewModel);
    }

    ViewBag.Result = false;
    return View(viewModel);
}

查看:

@if (ViewBag.Result)
{
    @section mySection{
        <script>
            $(function () {
                swal({
                    title: "Message",
                    text: "Your message has been sent successfully",
                    type: 'success',
                    showCancelButton: false,
                    allowOutsideClick: false,
                    confirmButtonColor: "green",
                    confirmButtonText: "Ok"
                }).then(function () {
                });
            })
        </script>
    }
}

【讨论】:

    猜你喜欢
    • 2017-08-30
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 2023-04-03
    相关资源
    最近更新 更多