在Asp.net MVC3中使用ajax的新消息警报

时间:2013-09-13 05:47:08

标签: javascript jquery ajax asp.net-mvc-3 ajax-polling

我确实有一个UI,我必须在新消息到达时显示警告。我正在尝试使用ajax这样做。我的意思是我有一个名为My Message的链接。我正在显示新的消息徽章新消息到来了。 我的代码如下:

<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;
    }

这必须在页面加载...所以如何在页面加载时使用ajax ...

<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 个答案:

答案 0 :(得分:0)

试试这个,始终使用Url.Action代替硬代码网址

<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>