Search code examples
c#asp.net-coreasp.net-core-3.1sweetalert2confirmation

Sweet alert confirmation dialog in asp.net core


I am new in using sweet alert in asp.net core. Here when I want to submit a form that store data in database after it adds a record I want to show a confirmation alert with sweet alert. Here it is my controller code:

 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);
    }

And here it is my code in view for Sweet alert inside my ViewBag Condition :

@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>
  }}

but when I submit the form although the form submit the information correctly but sweet alert doesn't work. Please refine my code so that the sweet alert work, I don't know why the sweet alert doesn't work inside my ViewBag condition?!


Solution

  • There are two problems in your codes.

    The first is mentioned by @kordiseps. You didn't return the View when ModelState.IsVaild is true, so the ViewBag value is always false, and the section will never be rendered.

    The second problem: the sweet alert works depends on the ViewBag condition, so here you should not do it in the form submit event. Instead, you need to do it in the page load function.

    Change your codes like below:

    Controller:

    [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);
    }
    

    View:

    @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>
        }
    }