【发布时间】:2017-11-07 06:59:15
【问题描述】:
我目前正在处理 MVC 5 中的数据表,但是当我尝试在数据表中加载内容时出现错误。
家庭控制器
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using CMS.Models;
using System.Linq.Dynamic;
using CMS.Models.DatabaseModels;
namespace CMS.Controllers.Home
{
public class HomeController : Controller
{
private CMSEntities db = new CMSEntities();
public ActionResult Index()
{
return View();
}
public ActionResult loaddata()
{
using (CMSEntities dc = new CMSEntities())
{
var data = dc.ContentItems.OrderBy(a => a.Name).ToList();
return Json(new { data = data }, JsonRequestBehavior.AllowGet);
}
}
}
}
索引.cshtml
ViewBag.Title = "Index";
}
<div style="width:90%; margin:0 auto;">
<table id="myTable">
<thead>
<tr>
<th>Name</th>
<th>Title</th>
<th>Create Date</th>
<th>Update Date</th>
<th>Item Content</th>
<th>Visual Order</th>
<th></th>
<th></th>
</tr>
</thead>
</table>
</div>
<style>
tr.even {
background-color: #F5F5F5 !important;
}
</style>
@* Load datatable css *@
<link href="//cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css" rel="stylesheet" />
@* Load datatable js *@
@section Scripts{
<script src="//cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function () {
$('#myTable').DataTable({
"ajax": {
"url": "/home/loaddata",
"type": "GET",
"datatype": "json"
},
"columns" : [
{ "data": "Name", "autoWidth": true },
{ "data": "Title", "autoWidth": true },
{ "data": "CreateDate", "autoWidth": true },
{ "data": "UpdateDate", "autoWidth": true },
{ "data": "ItemContent", "autoWidth": true },
{ "data": "VisualOrder", "autoWidth": true }
]
});
});
</script>
}
我得到的错误是:DataTables 警告:table id=myTable - Ajax 错误。有关此错误的更多信息,请参阅http://datatables.net/tn/7。
我去了网站,他们说需要检查错误消息。 该代码给了我 404 错误消息,这意味着 ajax 选项参数中的文件名和服务器上的文件中存在拼写错误。
我似乎找不到错误。
也许我错过了什么,但我不知道是什么。
【问题讨论】:
-
如果你把你的模型的代码
ContentItem:) -
您的 ajax 请求路径似乎出了点问题,请参阅控制台中的网络选项卡,看看它是否访问了正确的 url?
-
@NikunjPatel 内容项是从我在 SSMS 中创建的数据库中加载的
-
@Curiousdev 它去的路径是 /home/loaddata?_=1496743124550.. 我不认为这是正确的
-
@HJarry 你能放下你的json数据吗?
标签: c# jquery ajax asp.net-mvc datatable