Search code examples
asp.net-mvcmodel-view-controllerasp.net-mvc-2

how to display alert message in controller


I am using one controller which is inserting values in the database. I want to display alert message from controller when the values insertesd in the database successfully. Is it possible. If yes then how?


Solution

  • Basically that depends on how are you inserting the value into the database, as you would need a method to tells you whether the insertion was successful. As there's a few ways to do that now, linq/entity framework/sql/etc.

    Then after you know whether did the insertion happens, then you can just assign a value to a variable and then from the code/aspx just check the value and do a simple alert.

    <script type="text/javascript">
    //i'm using jquery ready event which will call the javascript chunk after the page has completed loading
    $(document).ready(function(){
    //assuming that your variable name from the code behind is bInsertSuccess
    var bSuccess = "<%= bInsertSuccess %>";
    if(bSuccess){
        alert("Successfully Inserted");
    }
    });
    </script>