DataTables warning: table id=tblData - Requested unknown parameter 'id' for row 0, column 1

DataTables warning: table id=tblData - Requested unknown parameter 'id' for row 0, column 1

anujpratap3anujpratap3 Posts: 5Questions: 1Answers: 0

I am stuck with this issue from couple of days. Can someone help me please?
Category Model:

public class Category
    {
        [Key]
        public int Id { get; set; }

        [Required]
        [Display(Name="Ennter category name")]
        [MaxLength(100,ErrorMessage ="Category name cannot exceed 200 characters")]
        public string Name { get; set; }
    }

Controller:

public IActionResult GetAll()
        {
            var result = _db.Category.GetAll();
            return Json(new { data = result });
        }

Category.js:

var dataTable;
$(document).ready(function () {
    loadDataTable();
});

function loadDataTable() {

    dataTable = $("#tblData").DataTable({
        'ajax': {
            "url": "/Admin/Category/GetAll",
            "type": "GET",
            "dataType": "JSON"
        },
        "columns": [
            {
                "data": "name",
            },
            {
                "data": "id",
                "render": function (arg) {
                    return
                    `<div class="text-center">
                    <a href="/Admin/Category/Upsert/${arg}" class="btn btn-success text-white" style="cursor:pointer;">
                        <i class="fas fa-edit"></i>
                    </a>
                    <a class="btn btn-danger text-white" style="cursor:pointer;">
                        <i class="fas fa-trash-alt"></i>
                    </a>
                    </div>`;
                }
            }
        ]
    });
}

I have no idea what is wrong with the code. In datatable name column is populating but other column with buttons are not populating and possibly throwing error.

Answers

  • anujpratap3anujpratap3 Posts: 5Questions: 1Answers: 0

    Here is the html past:

    <table id="tblData" class="table table-striped table-bordered" style="width:100%">
            <thead class="thead-dark">
                <tr class="table-info">
                    <th>Name</th>
                    <th></th>
                </tr>
            </thead>
        </table>
    
  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,735

    DataTables warning: table id=tblData - Requested unknown parameter 'id' for row 0, column 1

    You have "data": "id" which tells Datatables to look for an id object in your row data. Do you have an id object? If not you can use "data": null.

    Kevin

  • anujpratap3anujpratap3 Posts: 5Questions: 1Answers: 0

    Hi kthorngren, I have given the model please check above. Id column is present. I have debugged data is there both name and Id still it throws error.

  • anujpratap3anujpratap3 Posts: 5Questions: 1Answers: 0
    edited September 2021

    You can check screen shot to see the data.

  • anujpratap3anujpratap3 Posts: 5Questions: 1Answers: 0

    Hi, I have figured it out. Issue was because of the way I was creating html string in 2nd column 'render': function(){}. There I am returning html string inside tilde character so converted it into simple html string and working like usual. Thanks

Sign In or Register to comment.