【问题标题】:DataTables warning: table id=incomeDataTable - Ajax error. For more information about this error, please see http://datatables.net/tn/7DataTables 警告:table id=incomeDataTable - Ajax 错误。有关此错误的更多信息,请参阅 http://datatables.net/tn/7
【发布时间】:2021-05-03 12:58:02
【问题描述】:

我对 ASP .Net MVC 有点陌生,但仍处于学习模式。离开控制器后出现此错误。试图在这个论坛上找到解决方案,但不幸的是他们都没有帮助。 我的代码如下:

型号:

namespace TestHRMS3SIS.Models
{
    using System;
    using System.Collections.Generic;
    
    public partial class T11922H01
    {
        public int PMITHUniqueId { get; set; }
        public string PMITHIncomeId { get; set; }
        public string PMITHIncomeIdK { get; set; }
        public string PMITHDesc1 { get; set; }
        public string PMITHDesc2 { get; set; }
        public bool PMITHIsTaxable { get; set; }
        public string PMITHRuleId { get; set; }
        public Nullable<decimal> PMITHRentExemptPercent { get; set; }
        public Nullable<decimal> PMITHRentCityPercent { get; set; }
        public string PMITHIncomeCycle { get; set; }
        public byte PMITHPrintingSeq { get; set; }
        public string PMITHRoundingStrategy { get; set; }
        public Nullable<int> PMITHBICode { get; set; }
        public string PMITHBIDesc { get; set; }
        public int PMITHMarkForDeletion { get; set; }
        public string PMITHUser { get; set; }
        public System.DateTime PMITHDateTime { get; set; }
    
        public virtual T11922F01 T11922F01 { get; set; }
    }
}

查看

@{
    ViewBag.Title = "Income List";
}

<h2>Income List New</h2>

<table id="incomeDataTable">
    <thead>enter code here
        <tr>
            <th>IncomeId</th>
            <th>Dsc1</th>
            <th>Desc2</th>
            <th>RuleId</th>
        </tr>
    </thead>
</table>
<link href="//cdn.datatables.net/1.10.24/css/jquery.dataTables.min.css" rel="stylesheet" />
@section scripts
{
    <script src="//cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#incomeDataTable").DataTable(
                {
                    "processing": true,
                    "serverSide": true,
                    "ajax": {
                        "url": "/DataTableTest/GetIncomeList",
                        "type": "GET",
                        "datatype": "json"
                    },
                    "columns": [
                        { "data": "PMITHIncomeId" },
                        { "data": "PMITHDesc1" },
                        { "data": "PMITHDesc2" },
                        { "data": "PMITHRuleId" }
                    ]
                });
        });
    </script>
}

控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestHRMS3SIS.Models;

namespace TestHRMS3SIS.Controllers.DataTableTest
{
    public class DataTableTestController : Controller
    {
        // GET: DataTable
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetIncomeList()
        {
            using (Entities3SIS oDbConnection = new Entities3SIS())
            {
                var oT11922H01 = oDbConnection.T11922H01.ToList<T11922H01>();
                return Json(new { data = oT11922H01 }, JsonRequestBehavior.AllowGet);                
            }

        }
    }
}

我在控制器中设置了一个断点,并观察到在返回语句中我收到了这个错误。

感谢任何帮助。

【问题讨论】:

    标签: asp.net-mvc asp.net-core datatable


    【解决方案1】:

    如下更改列部分:

    "columns": [
        { "data": "pmithIncomeId" },
        { "data": "pmithDesc1" },
        { "data": "pmithDesc2" },
        { "data": "pmithRuleId" }
    ]
    

    返回数据为驼峰式。

    【讨论】:

    • 谢谢 mj。这没有帮助。上述字段中的前 5 个字母是 SQL 表中每个字段的前缀。我试着改变你的建议。我仍然遇到同样的错误。
    • 嗨@ShishirDonde,请调试这个方法GetIncomeList,好像有错误。
    • 不,它没有任何错误。我可以使用变量 oT11922H01 中的 12 行列表来返回语句。按 F10 后返回 Json(new { data = oT11922H01 }, JsonRequestBehavior.AllowGet);我收到此错误。
    • @ShishirDonde,你可以使用postman之类的工具,或者直接在浏览器中输入url(.../DataTableTest/GetIncomeList)来测试这个方法,检查它是否返回正确的数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 2019-08-15
    相关资源
    最近更新 更多