MVC: show Modal dialog from controller

Can show a modal dialog box inside a controller.

eg:

[HttpPost]
        public ActionResult Index(int contractType )
        {
            if (contractType == 0 )
            {
                return "SHOW MODALDIALOG BOX" with button "YES" and "NO" when click "YES" Refirect to nexe page, click "NO" stay in current page
            }
            else
            {
                return View();
            }
        }

      

thanks for answers

+3


source to share


2 answers


From Controller (Server), you cannot display the popup. What you can do is return the view ... with some flag to make the VIEW display the javascript modal dialog.

Another option returns JSON instead of view ... and creates a modal dialog using JS. Then ... on clic YES, you can call the same controller action with a different parameter (in your case, something other than "0"), and this time display the View.

Example:

[HttpPost]
    public ActionResult Registering( )
    {

        string RetResult =  new UserPermission().ValidateUser( Request["username"].ToString(), Request["password"].ToString() );

        if (!string.IsNullOrEmpty(RetResult)){
            ViewBag.MyErrorMessage = RetResult;
            return View(); //This will show the view named "Registering", but you can display any other.
//The ideal is display the same one where the user entered the user/pass.
        }
        else {
            return RedirectToAction("Index", "EvalMain"); 
        }
    }

      

In your VIEW:

@if(ViewBag.MyErrorMessage != null){
     //Display the error message
     //You can: display it in a div (without popup), you can show a javascript Alert(), or display a modal dialog.    
}

      



To display it as a DIV, just do:

<div>@ViewBag.MyErrorMessage </div>

      

To display a warning ():

<script> alert(@ViewBag.MyErrorMessage);</script>

      

To display the Modal Dialog , you can use jQueryUI: http://jqueryui.com/dialog/ More or less something like this:

<div id="dialog" title="Error Registering">
<p>@ViewBag.MyErrorMessage</p>
</div>

<script>
    $(function() {
    $("#dialog").dialog();
    });
</script>

      

+3


source


Thanks for answers. The idea is very simple. I am starting the eMemberShip WCF service which accepts a username and password and also returns the user's role and permissions. If the username or password is not correct, the returned service returns an error string and I want to show the ModalDialog with that error string, and if not, redirect another page.



[HttpPost]
    public ActionResult Registering( )
    {

        string RetResult =  new UserPermission().ValidateUser( Request["username"].ToString(), Request["password"].ToString() );



        if (!string.IsNullOrEmpty(RetResult))
            return this.ModalDialog(RetResult);

        else
            return RedirectToAction("Index", "EvalMain"); 
    }

      

0


source







All Articles