New message alert using ajax in Asp.net MVC3

584 views Asked by At

I do have a UI where I have to show the alert when ever new message arrives.I am trying to do this using ajax.I mean I do have a link called as My Message.I am showing new message badge when ever new message arrives. My code goes as follows:

<li> @Html.ActionLink("My Messages", "MyMessage", "ForumPost") </li>    
    var IsRead = "false";
    if ( IsRead == "true" ) {
       <span id="txtblnk" class="badge badge-success">New_true</span>
    } else {
       <span id="txtblnk" class="badge badge-success">New Message</span>
    }        
    .badge-success {
        background-color: #F80D0D;
    }
    .label-success[href], .badge-success[href] {
        background-color: #F80D0D;
    }
    .badge {
        display: inline-block;
        padding: 2px 4px;
        font-size: 11.844px;
        font-weight: bold;
        line-height: 14px;
        color: #1EEE2E;
        text-shadow: 0 -1px 0 rgba(0,0,0,0.25);
        white-space: nowrap;
        vertical-align: baseline;
        background-color: #F80D0D;
    }
    .badge {
        padding-right: 9px;
        padding-left: 9px;
        -webkit-border-radius: 9px;
        -moz-border-radius: 9px;
        border-radius: 9px;
    }

This must be on page load...so how to use ajax on page load...

<script type="text/javascript">
        var check;
        function checkForMessages() {
            $.get("/action/controller", 
            function ( data ) {
//what should I check here from controller?
               if ( ) {
                  //There are new messages
                  clearInterval( check );
                  alert("You have mail!");
               }
            }
        }
        check = setInterval( checkForMessages, 60000 );
    </script>
1

There are 1 answers

5
Amit On BEST ANSWER

Try this, Always use Url.Action instead of hard code URL

<script type="text/javascript">
        var check;
        function checkForMessages() {
            $.get('@Url.Action("ActionName","ControllerName")', 
            function ( data ) {
//what should I check here from controller?
               if ( ) {
                  //There are new messages
                  clearInterval( check );
                  alert("You have mail!");
               }
            }
        }
        check = setInterval( checkForMessages, 60000 );
</script>