【问题标题】:ASP.net MVC Data Table Ajax ErrorASP.net MVC 数据表 Ajax 错误
【发布时间】:2017-06-12 02:21:55
【问题描述】:

我正在创建我的第一个 MVC 应用程序,这个应用程序应该在数据表中播放存储在我的表“客户端”中的所有信息,这个表是使用 SQL Server 创建的,并且正在使用实体框架链接到我的项目。

但是当我运行我的代码时,我收到了这个错误消息:

DataTables warning: table id=myDataTable - Ajax error. For more information about this error, please see http://datatables.net/tn/7


@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" class="tablecontainer" />
    <title>Index</title>
    <!--Hojas de estilo ( Archivos planos de texto para poner estilos a mis etiquetas)-->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css" />
    <link href="~/Content/themes/base/jquery-ui.min.css" rel="stylesheet" />
    <link rel="stylesheet" href="~/Style/miprimerStyle.css" />
</head>
<body>
    <!--Ancho del padre-->
    <div class="tamaño" id="tamaño"> 
        <!--a es hipervinculos-->
        <a class="popup btn btn-primary Margin20" href="/home/save/0" >Agregar un nuevo cliente </a>
        <table id="myDataTable">
            <thead>
                <!--Fila-->
                <tr>
                    <!--Columna-->
                    <th>Nombre</th> 
                    <th>Apellido</th>
                    <th>Telefono</th>
                </tr>
            </thead>
        </table>
    </div>
    <script src="~/Scripts/jquery-3.1.1.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
    <script src="~/Scripts/jquery-ui-1.12.1.min.js"></script>
    <script>
        $(document).ready(function () {
            var oTable = $('#myDataTable').DataTable({
                "ajax": {
                    "url" : '/home/GetEmployees',
                    "type" : "get",
                    "datatype" : "json"
                },
                "columns": [
                    { "data": "Nombre", "autoWidth": true },
                    { "data": "Apellido", "autoWidth": true },
                    { "data": "Telefono", "autoWidth": true },
                ]
            })
        })
    </script>
</body>
</html>

这是我的家庭控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace TestDeConocimientos.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetEmployees()
        {
            using (TerasysDBEntities1 dc = new TerasysDBEntities1())
            {
               var clientes = dc.Clientes.OrderBy(a => a.Nombre).ToList();
               return Json(new { data = clientes }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}

我还在该行下了一个断点:

var clientes = dc.Clientes.OrderBy(a => a.Nombre).ToList();

我可以看到所有数据都完美地存储在 Var“客户端”中

这里发生了什么?

【问题讨论】:

  • 根据报错信息,Ajax调用失败。 datatables.net/tn/7 页面解释了如何从浏览器获取 Http 错误。请按照这些说明在此处发布信息。
  • 检查一下,它可能会有所帮助:codeproject.com/Articles/1114208/…
  • @derloopkat 2jquery.validate.unobtrusive.min.js 加载资源失败:服务器响应状态为 404(未找到):3423/home/GetEmployees?_=1497233865564 加载资源失败: 服务器响应状态为 500 (Internal Server Error)
  • 尝试注释掉两行:
  • @Tomato32 Stills 不工作

标签: c# asp.net ajax asp.net-mvc


【解决方案1】:

已解决:

public ActionResult GetEmployees()
        {
            using (TerasysDBEntities1 dc = new TerasysDBEntities1())
            {
                dc.Configuration.LazyLoadingEnabled = false;
                var clientes = dc.Clientes.OrderBy(a => a.Nombre).ToList();
                return Json(new { data = clientes }, JsonRequestBehavior.AllowGet);
            }
        }

【讨论】:

    猜你喜欢
    • 2013-03-03
    • 2011-06-10
    • 2017-12-30
    • 2020-03-30
    • 1970-01-01
    • 2013-02-27
    • 2010-11-05
    • 2016-01-07
    • 1970-01-01
    相关资源
    最近更新 更多