Gman16
Gman16

Reputation: 183

Display popup dialog box when user clicks button using MVC3 C# asp.net

Can somebody please explain how I can create a dialog box that will appear when the user clicks a button on a webpage. I am using the MVC3 framework coding in c# and asp.net.

Basically, when the user clicks 'Send' - the dialog box will display "message has been sent" and they can close it. In the instance that there were validation errors the dialog box should display it (e.g: "Please enter a valid email address and try again").

Kindly explain exactly what code needs to go in which files. i.e. controller, view, model, scripts, css, etc...

Thanks

Upvotes: 0

Views: 9189

Answers (3)

Nicolai
Nicolai

Reputation: 1953

you can use jQuery UI modal:

  <script>
  $(function() {
    $( "#dialog-message" ).dialog({
      modal: true,
      buttons: {
        Ok: function() {
          $( this ).dialog( "close" );
        }
      }
    });
  });
  </script>

<div id="dialog-message" title="Download complete">
  <p id="messageText">
  </p>
</div>

And after click on button you can send ajax request and on success set message in this dialog and show it.

EDIT:

<input type = "submit" id="doSomethingButton">

<script>
    $(function(){
        $('#doSomethingButton').click(function(){
            $.ajax({
                url:"url-to-send-data",
                data:"", // optional
                type:"http-method-type", //GET, POST, DELETE, PUT ....
                success:function(data){
                    $('#messageText').text(data);
                    $('#dialog-message').dialog('open');
                }
            });
        });
    });
</script>

Upvotes: 1

Rajshekar Reddy
Rajshekar Reddy

Reputation: 18987

See these JQuery plugin's Here this is of sure help

Upvotes: 0

pid
pid

Reputation: 11607

You have to use Javascript. From C# you should have an OnClientClick event. Put there you scriptlet like this:

OnClientClick="alert('Hello World!');"

Return true or false base on if you want the server-side click to happen or not:

OnClientClick="return confirm('Are you sure?');"

If this code is not exactly perfect it will still lead you to the correct solution, because basically there aren't any other options really.

Upvotes: 1

Related Questions