How I can open popup window without using Windows MessageBox?

OneWorld 0 Reputation points
2024-02-09T12:41:15.78+00:00

The MessageBox below shows the alert in local (Visual Studio) but not on a server call. Is there a JavaScript, MVC Controller or ASP.NET form popup windows I can use on the server ?

if (helperClass.IsTheUserExist(Employee.Email, Employee.ID))
{ helperClass.UpdateUserInformation(Employee.Email, Employee.ID); }
else
  {
    MessageBox.Show ("Please, update your account");
  } 
Microsoft 365 and Office Development Office JavaScript API
Developer technologies .NET Other
Developer technologies ASP.NET Other
Developer technologies C#
{count} votes

4 answers

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2024-02-09T14:13:30.26+00:00

    Is there a JavaScript, MVC Controller or ASP.NET form popup windows I can use on the server?

    The easiest thing to do is return a View that shows the message.

    [HttpGet]
    public ActionResult Index()
    {
        ViewBag.Message = "Please, update your account";
        return View();
    }
    

    The view checks if there is a message. If the message exists, then the view shows the message.

    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    <h2>Index</h2>
    <div>
        @if (ViewBag.Message != null)
        {
            <div class="alert alert-danger" role="alert">
                @ViewBag.Message
            </div>
        }
    </div>
    

    The same concept applies to a popup or modal. The different is rather than writing dynamic HTML you'll write dynamic JavaScript.


  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2024-02-09T17:07:37.0766667+00:00

    the javascript equivalent to a message box is the alert() and confirm() functions. you can also use a javascript and bootstrap like above, or pure javascript:

    https://www.w3schools.com/howto/howto_js_popup.asp

    the issue you face common to all solutions, is you can not call the message box from the server, you set a flag, so that the html/javascript code is included in the produced html. when the browser loads the response html, it will display the messagebox.

    0 comments No comments

  3. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2024-02-10T18:39:47.6833333+00:00

    Another choice is sweetalert. Simple

    Swal.fire("Please update your accoun");
    

    More control

    function messageBox() {
        (async () => {
    
            const swalWithBootstrapButtons = Swal.mixin({
                customClass: {
                    confirmButton: 'btn btn-primary'
                },
                buttonsStyling: false
            });
    
            swalWithBootstrapButtons.fire({
                title: 'Information',
                html: "<strong>Please update your account</strong>",
                confirmButtonText: 'OK',
                allowOutsideClick: false,
            }).then((result) => {
                if (result.isConfirmed) {
                    // do nothing
                } 
            });
        })();
    };
    
    0 comments No comments

  4. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2024-02-12T07:10:02.6966667+00:00

    Hi @OneWorld,

    Based on the code you provided, you should want to implement the MessageBox in MVC.

    You can try the code below.

    <script src="~/Scripts/jquery-3.7.1.min.js"></script>
    <div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog err-pop" style="">
            <div class="modal-content">
                <div class="modal-header">
                    <button id="DivClose" type="button" class="close" data-bs-dismiss="modal" aria-label="Close">&times;</button>
                </div>
                <div class="modal-body" style="text-align:center;">
                    <p style="color: red;">@TempData["ProcessMessage"]</p>
                </div>
            </div>
        </div>
    </div>
    @if (TempData["message"] != null)
    {
        var modal = TempData["message"].ToString();
        <script type='text/javascript'>
            $(document).ready(function(){
                $('#@modal').modal('show');
            });
        </script>
    }
    
    public ActionResult Test4()
     {
         TempData["ProcessMessage"] = "Please, update your account";
         TempData["message"] = "myModal";
         return View();
     }
    

    System.InvalidOperationException: 'The RouteData must contain an item named 'action' with a non-empty string value.

    you need to add route data. Exception tell you everything.

    var routeData = new RouteData() ;
    routeData.Values.Add("Controller", "Home");
    routeData.Values.Add("Action", "Index");
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.