Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
625 views
in Technique[技术] by (71.8m points)

c# - Json result is not displaying

    I am facing issue while displaying the result from a ajax call.
    Contrller json method which returns data from database.

The control is coming in the success block of my ajax call but the data is empty. if i debug the controller GetNotifications method it is returning the data. Can somebody help me with this

     public JsonResult  GetNotifications()
            {

                IList<Notification> notificationsBody = db.Notifications.ToList();

                return Json(new { notificationsBody = notificationsBody }, JsonRequestBehavior.AllowGet); 

            }`View used to display and call ajax is 

@model IEnumerable<NotificationApp.Model.Notification> 
<h2>
    Notification</h2>
@using (Html.BeginForm())
{
    <ul id="nav">
        <li id="notification_li"><span id="notification_count"></span><a href="#" id="notificationLink">
            Notifications</a>
            <div id="notificationContainer">
                <div id="notificationTitle">
                    Notifications</div>
                <div id="notificationsBody" class="notifications">
                </div>
                <div id="notificationFooter">
                    <a href="#">See All</a></div>
            </div>
        </li>
    </ul>
}
@section Scripts {
    <script src="~/Scripts/jquery-2.1.4.min.js" type="text/javascript"></script>
    <script src="~/Scripts/jquery.signalR-1.1.4.js" type="text/javascript"></script>
    <script src="~/signalr/hubs"></script>
    <script type="text/javascript">
        $(function () {
            var proxy = $.connection.notificationHub;
            proxy.client.receiveNotification = function (message, UnreadCount) {
                $("#notification_count").html(UnreadCount);
                $("#notification_count").show();
                $("#container").slideDown(2000);
                setTimeout('$("#container").slideUp(2000);', 5000);
            };
            $.connection.hub.start();

        });
    </script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#notificationLink").click(function () {
                $("#notificationContainer").fadeToggle(300);
                $("#notification_count").fadeOut("slow");

                var tbl = $('#notificationsBody');

                $.ajax({
                    url: '/Notification/GetNotifications',
                    contentType: 'application/json ; charset:utf-8',
                    type: 'POST',
                    dataType: 'json'
                }).success(function (result) {
                    alert(1);
                    alert(result[0].NotificationID);
                    tbl.empty().append(result);
                }).error(function () {
                    alert(13);
                });

                //success: function (data) {
                //if (data) {
                //var len = data.length;
                //var txt = "";
                //if (len > 0) {
                //for (var i = 0; i < len; i++) {
                //if (data[i].Name && data[i].Address) {
                //txt += "<tr><td>" + data[i].Name + "</td><td>" + data[i].Address + "</td></tr>"
                //  + "<td>" + data[i].PhoneNo + "</td>" + "<td>" + data[i].Country + "</td></tr>";
                //}
                //}
                //if (txt != "") {
                //$("#EmployeesTable").append(txt);
                //alert(txt);
                //}
                //}
                //}
                //},



            });
            return false;

            //Document Click hiding the popup 
            $(document).click(function () {
                $("#notificationContainer").hide();
            });

            //Popup on click
            $("#notificationContainer").click(function () {
                return false;
            });

        });
    </script>
}
`

here i am getting the Alert which is inside the success block. But the line alert(result[0].NotificationID); is not displaying any alert.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since I don't have enough reputation I can't write this as an comment:

I think, your question is unanswered because it is a bit unclear, what you are asking for..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...