Hi makumbi,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Controller
Public Class HomeController
Inherits Controller
' GET: Home
Function Index() As ActionResult
Return View()
End Function
<HttpPost>
Public Function AjaxMethod() As JsonResult
Dim entities As New NorthwindEntities
Dim customers As List(Of Customer) = (From customer In entities.Customers).ToList()
Return Json(customers)
End Function
End Class
View
@Code
Layout = Nothing
End Code
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div style="width: 500px">
<table id="tblCustomers" cellpadding="0" cellspacing="0" border="1" style="border-collapse:collapse">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Designation</th>
<th>Address</th>
<th>City</th>
<th>Country</th>
<th>Phone</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.css" />
<script type="text/javascript">
$(function () {
$.ajax({
type: "POST",
url: "/Home/AjaxMethod",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
});
function OnSuccess(response) {
$("#tblCustomers").DataTable({
bLengthChange: true,
lengthMenu: [[5, 10, -1], [5, 10, "All"]],
bFilter: true,
bSort: true,
bPaginate: true,
data: response,
columns: [
{ 'data': 'CustomerID' },
{ 'data': 'ContactName' },
{ 'data': 'ContactTitle' },
{ 'data': 'Address' },
{ 'data': 'City' },
{ 'data': 'Country' },
{ 'data': 'Phone' }
]
});
};
</script>
</body>
</html>
Screenshot