[英]Rendering json data into a grid in asp.net mvc
I am looking to populate my grid with JSON data. 我想用JSON数据填充网格。 The data is in the below format:
数据格式如下:
[{
"SiteId":"1",
"SiteName":"University of Minessota",
"SiteStatus":"Fully Operational"
},{
"SiteId":"2",
"SiteName":"MSP Airport-Humphrey",
"SiteStatus":"Settlement Required"
},{
"SiteId":"3",
"SiteName":"City Of Minneapolis-Lot C",
"SiteStatus":"Fully Operational"
},{
"SiteId":"4",
"SiteName":"TargetCenter",
"SiteStatus":"Low Tickets"
},{
"SiteId":"5",
"SiteName":"Excel Energy Center",
"SiteStatus":"Out Of Tickets"
}]
and i am passing this to the view using my controller method: 我正在使用我的控制器方法将此传递给视图:
public JsonResult StatusReport()
{
List<CenterStatus> status = MvcList.Models.CenterStatus.GetStatus();
return this.Json(status, JsonRequestBehavior.AllowGet);
}
Now in my view I need to populate this JSON data into a grid: 现在在我看来,我需要将此JSON数据填充到网格中:
$(document).ready(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "CenterStatus/StatusReport.aspx",
data: "{}",
dataType: "json",
success: function (data) {
for (var i = 0; i < data.d.length; i++) {
$("#GridView1").append("<tr><td>" + data.d[i].siteID + "</td><td>" + data.d[i].siteName + "</td><td>" + data.d[i].siteStatus + "</td></tr>");
}
},
error: function (result) {
alert("Error");
}
});
But I have no luck in achieving my goal so far. 但是到目前为止我没有实现我的目标的运气。 Any suggestions?
有什么建议么?
Do it like this. 像这样做。 do not call
append
in a loop. 不要在循环中调用
append
。 Set it to a variable and call the html
function only once. 将其设置为变量,然后仅调用
html
函数一次。
success: function (data) {
var row=""
$.each(data,function(index,item){
row+="<tr><td>"+item.SiteName+"</td></tr>";
});
$("#GridView1").html(row);
},
Working sample : http://jsfiddle.net/x76LD/1/ 工作示例: http : //jsfiddle.net/x76LD/1/
可能您应该在成功函数中仅使用data
而不是data.d
I'm guessing the url: "CenterStatus/StatusReport.aspx",
might have something to do with it. 我猜
url: "CenterStatus/StatusReport.aspx",
可能与它有关。 The correct url should probably be: 正确的网址应该是:
url: "/CenterStatus/StatusReport"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.