C# Json结果未显示
控件出现在ajax调用的成功块中,但数据为空。如果我调试控制器GetNotifications方法,它将返回数据。有人能帮我吗C# Json结果未显示,c#,json,asp.net-mvc,signalr,asp.net-ajax,C#,Json,Asp.net Mvc,Signalr,Asp.net Ajax,控件出现在ajax调用的成功块中,但数据为空。如果我调试控制器GetNotifications方法,它将返回数据。有人能帮我吗 I am facing issue while displaying the result from a ajax call. Contrller json method which returns data from database. publicjsonresult GetNotifications() { IList notifications
I am facing issue while displaying the result from a ajax call.
Contrller json method which returns data from database.
publicjsonresult GetNotifications()
{
IList notificationsBody=db.Notifications.ToList();
返回Json(新的{notificationsBody=notificationsBody},JsonRequestBehavior.AllowGet);
}`用于显示和调用ajax的视图是
@模型IEnumerable
通知
@使用(Html.BeginForm())
{
}
@节脚本{
$(函数(){
var proxy=$.connection.notificationHub;
proxy.client.receiveNotification=函数(消息,未登录){
$(“#通知计数”).html(未计数);
$(“通知计数”).show();
$(“#容器”)。向下滑动(2000年);
setTimeout(“$”(“#容器”).slideUp(2000);”,5000);
};
$.connection.hub.start();
});
$(文档).ready(函数(){
$(“#notificationLink”)。单击(函数(){
$(“通知容器”).fadeToggle(300);
$(“通知计数”)。淡出(“缓慢”);
变量tbl=$('notificationsBody');
$.ajax({
url:“/Notification/GetNotifications”,
contentType:'application/json;字符集:utf-8',
键入:“POST”,
数据类型:“json”
}).成功(功能(结果){
警报(1);
警报(结果[0]。通知ID);
tbl.empty().append(结果);
}).错误(函数(){
警觉(13);
});
//成功:功能(数据){
//如果(数据){
//var len=data.length;
//var txt=“”;
//如果(len>0){
//对于(变量i=0;i
这里我得到了成功块中的警报。但是这条线
警报(结果[0]。通知ID);
没有显示任何警报。由于我没有足够的声誉,我不能将此作为评论:
我想,你的问题没有得到回答,因为它有点不清楚,你在问什么。首先不要使用警告对话框会很烦人使用console.log(result)或console.log(JSON.stringify(result))并在chrome中按f12键查看控制台。若并没有返回,控制器中可能会出现问题。 核实
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>
}
`
您只需发送对象,无需执行notificationsBody=notificationsBody
获取列表时,使用$。每个方法迭代数据
我不知道为什么人们在不向服务器发送数据的情况下在ajax中使用这种方法
return Json(notificationsBody , JsonRequestBehavior.AllowGet);
只需使用您需要的参数。当我试图发布问题时,它会发出一个警告,提示我的问题没有得到回答。有人能告诉我如何排序吗?我的问题没有得到回答。问问题时我是否遗漏了什么。有人能告诉我为什么这里没有回答这些问题吗?
contentType: 'application/json ; charset:utf-8',