如何使用javascript打印通用列表?
好吧,假设我得到了一个javascript,它将时间发送到另一个.asp页面,并根据发送的时间从数据库接收记录列表 这是javascript:如何使用javascript打印通用列表?,javascript,asp.net-mvc,razor,Javascript,Asp.net Mvc,Razor,好吧,假设我得到了一个javascript,它将时间发送到另一个.asp页面,并根据发送的时间从数据库接收记录列表 这是javascript: $("button").click(function () { $.get("Empty2.cshtml",{ time:"6:30", },function (data, status) { // wanna print out the data
$("button").click(function () {
$.get("Empty2.cshtml",{
time:"6:30",
},function (data, status) {
// wanna print out the data received here
});
});
这是“清空2”页,需要花费时间并发回记录列表:
@{
hutsDBEntities db = new hutsDBEntities();
var Time = Request["time"];
var tt = db.Trips.Where(u=>u.Time == Time).ToList();;
Response.Write(tt);
}
trips是数据库中有许多列的表(包括ofc的“时间”列)
我的问题是:如何在页面上的任何位置以javascript打印对象(数据)的内容?您可以将列表作为JSON返回:
@{
hutsDBEntities db = new hutsDBEntities();
var Time = Request["time"];
var tt = db.Trips.Where(u=>u.Time == Time).ToList();;
Response.ContentType = "application/json";
Response.Write(Json.Encode(tt));
}
然后在AJAX请求的成功回调中,您将能够访问结果:
$("button").click(function () {
$.get("Empty2.cshtml", { time:"6:30" }, function (data, status) {
alert(JSON.stringify(data));
});
});
如果您想以表格形式显示结果,而不是仅仅通知原始JSON,您可以使用如下方式:
$.get("Empty2.cshtml", { time:"6:30" }, function (data, status) {
var result = '<table>';
$.each(data, function() {
// In this example TripName and TripLocation must be properties of the
// returned JSON array
result += '<tr><td>' + this.TripName + '</td><td>' + this.TripLocation + '</td></tr>';
});
result += '</table>';
$('body').append(result);
});
$.get(“Empty2.cshtml”,{time:“6:30”},函数(数据,状态){
var结果=“”;
$.each(数据,函数(){
//在本例中,TripName和TripLocation必须是的属性
//返回的JSON数组
结果+=''+this.TripName+''+this.TripLocation+'';
});
结果+='';
$('body')。追加(结果);
});