簡體   English   中英

無法將數據綁定到 ASP.NET MVC 中的數據表

[英]Not able to bind data to datatable in ASP.NET MVC

下面是我的 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;
}

怎么了? 我無法弄清楚。

您是否閱讀過文檔: 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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM