【发布时间】:2019-06-03 10:36:24
【问题描述】:
尝试使用 jquery 和 ajax 创建一个表,但我在设置它时遇到问题,使其看起来像下面的输出。我似乎遇到的主要问题是从我的控制器获取返回以传递到我应该创建表的视图中。我不太确定如何通过控制器将数据传递到视图页面,然后对其进行操作以形成表格。我的json如下:
[
{
"category": 1,
"speedCategory": "0-3km/h",
"year": 2015,
"count": 17008
},
{
"category": 2,
"speedCategory": "3-6km/h",
"year": 2015,
"count": 4,694
},
{
"category": 1,
"speedCategory": "0-3km/h",
"year": 2016,
"count": 12546
},
{
"category": 2,
"speedCategory": "3-6km/h",
"year": 2016,
"count": 500
}
等等。
这是我用来尝试返回信息的控制器: `
public class SpeedWebAPIController : ApiController
{
private SpeedEntities db = new SpeedEntities();
[HttpGet]
public object GetSpeedWebAPI()
{
var speedCodes = (
from ec in db.speedCodes
join e in db.Speed
on ec.speedTimeCode equals e.speedTimeCode
select new
{
ec.category,
ec.speedCategory,
e.year
})
.GroupBy(p => new
{
p.category,
p.speedCategory,
year = p.Year
})
.Select(p => new
{
p.Key.category,
p.Key.speedCategory,
p.Key.year,
Count = p.Count()
})
.OrderBy(p => p.year)
.ThenBy(p => p.category)
.ToList();
return speedCodes;
}`
我正在尝试使用页面底部的 jquery 将其附加到我的表中: `
<div class="row">
<div class="col-4" id="table">
</div>
<script>
//Not sure how to pass in controller return and then create a jquery table
//with this
</script>`
表格输出应如下所示:
+--------------+-------------+
| Speed | Count |
+--------------+-------------+
| 2015 |
+--------------+-------------+
| 0 to 3km/h | 17,008 |
+--------------+-------------+
| 3km/h to 6km | 4,694 |
+--------------+-------------+
| 6km/h to 10km| 4,895 |
+--------------+-------------+
| 2016 |
+--------------+-------------+
| 0 to 3km/h | 12,546 |
+--------------+-------------+
等等。 所以它有速度和计数的整体表标题,然后是年份的子标题。
【问题讨论】:
标签: jquery ajax visual-studio