【问题标题】:Not able to bind data to datatable in ASP.NET MVC无法将数据绑定到 ASP.NET MVC 中的数据表
【发布时间】:2020-10-03 06:10:21
【问题描述】:

下面是我的 ASP.NET MVC 项目中的代码。我正在使用数据表来创建一个表。我正在从 Web API 获取数据。正在返回数据,但在绑定时出现此处显示的错误。我尝试删除很多有按钮的代码。现在我只有简单绑定它的代码了。

数据表警告:表 id=患者 - ajax 错误。有关此错误的更多信息,请参阅http://datatables.net/tn/7

jQuery 代码:

$(document).ready(function () {
            debugger;
            var table = $("#patients").DataTable({
                
                ajax: {
                    
                    url: "/api/patients",
                    dataSrc: ""
                },
                columns: [
                    
                    {
                        data: "First_Name"
                    },
                    {
                        data: "phoneNumber",
                        render: function (data) {
                            debugger;
                            return data.toString().replace(
                              /(\d\d\d)(\d\d\d)(\d\d\d\d)/g, '$1-$2-$3');
                        }
                    },
                    {
                        data: "Address"
                    },
]
            });
 });

来自控制器的 API 代码:

public IHttpActionResult GetPatients()
{
    var patientDto = getdata();
    return Ok(patientDto);
}

public IEnumerable<Patient_Response> getdata()
{
    IEnumerable<Patient_Response> students = null;

    using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Add("Authorization", "Bearer 0f6af107-6ad2-4665-ad24-f09402d50082");
        client.BaseAddress = new Uri("http://localhost:6600/api/");

        // HTTP GET
        var responseTask = client.GetAsync("patients");
        responseTask.Wait();

        var result = responseTask.Result;

        if (result.IsSuccessStatusCode)
        {
            var readTask = result.Content.ReadAsAsync<IList<Patient_Response>>();
            readTask.Wait();

            students = readTask.Result;
        }
        else //web api sent error response 
        {
            // log response status here..
            students = Enumerable.Empty<Patient_Response>();

            ModelState.AddModelError(string.Empty, "Server error. Please contact administrator.");
        }
    }

    return students;
}

怎么了?我想不通。

【问题讨论】:

    标签: c# jquery datatable webapi


    【解决方案1】:

    您是否阅读过文档:https://datatables.net/manual/tech-notes/7

    当 jQuery 落入其错误回调处理程序(此回调内置于 DataTables)时会发生这种情况,这通常会在服务器以 2xx HTTP 状态代码以外的任何内容响应时发生。

    这意味着你的呼叫去了控制器,没有带来任何数据。

    您可以使用下面的代码来查看问题所在:

    $.fn.dataTable.ext.errMode = 'none';
     
    $('#patients')
        .on( 'error.dt', function ( e, settings, techNote, message ) {
            alert( 'An error has been reported by DataTables: ', message );
        } )
        .DataTable();
    

    【讨论】:

    • 看来我正在返回 Ienumerable ,这在视图方面没有被接受。我怎样才能将它转换为一些可以接受的集合?
    • return students.ToList();
    • 现在从服务器端正确返回。但仍然不知道 View 端出了什么问题
    猜你喜欢
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    • 2020-05-28
    • 2019-06-02
    相关资源
    最近更新 更多