DataTable doesn't effect on the table - c#

I am using DataTable library in MVC, but it doesn't work
this is the view Part
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>Id</th>
<th>Phone</th>
<th>FirstName</th>
<th>LastName</th>
</tr>
</thead>
<tbody>
</tbody>
<tfoot>
<tr>
<th>Id</th>
<th>Phone</th>
<th>FirstName</th>
<th>LastName</th>
</tr>
</tfoot>
</table>
<script type="text/javascript">
//$(document).ready(function () {
// $('#example').DataTable();
//})
$(document).ready(function () {
GetEmployeeRecord();
})
var GetEmployeeRecord = function () {
$.ajax({
type: "Get",
url: "/BasicInfo/GetCustomers",
success: function (response) {
alert("success");
BindDataTable(response);
},
error: function () {
alert("error");
}
})
}
var BindDataTable = function (response) {
$("#example").DataTable({
"aaData": response,
"aoColumns": [
{ "mData": "Id" },
{ "mData": "Phone" },
{ "mData": "FirstName" },
{ "mData": "lastName" }
]
});
}
and this is Controller Part
public JsonResult GetCustomers()
{
SalonEntities db = new SalonEntities();
List<Customer> CustomerList = db.Customers.OrderByDescending(a => a.ID).ToList();
return Json(CustomerList , JsonRequestBehavior.AllowGet);
}
although the CustomerList in controller is loaded but it returns to the view it enters the error section of jquery
what could be the problram

The ajax call to your controller failed. To debug this, you can do two things:
Alert the error
Remove the (dobule quote) " to alert the actual error.
error: function () {
alert(error);
}
You will most probably be able to see it in developer tools in your console.
Check the network tab
In your network tab, you'll see the error (4XX, 5XX) or something like that with some extra information (if you are lucky).
If no more information is there and you have an error 500, you need to check the exception in your backend code.

Related

Jquery Data Table - No data available in table

I have developed an application using .net core. One of my requirements is datatables. In my controller I am calling a REST Api service and returning a Json result. The view is pretty straight forward in that of my html table and the AJAX call to the controller for data. Upon execution of the application my datatable shows up with 'No data available in table'
<table id="datatable">
<thead class="thead-light">
<tr role="row">
<th class="table-column-pr-0 sorting_disabled" rowspan="1" colspan="1" aria-label="" style="width: 44px;">
<div class="custom-control custom-checkbox">
<input id="datatableCheckAll" type="checkbox" class="custom-control-input">
<label class="custom-control-label" for="datatableCheckAll"></label>
</div>
</th>
<th class="table-column-pl-0 sorting" tabindex="0" aria-controls="datatable" rowspan="1" colspan="1" style="width: 299px;">UserID</th>
<th class="table-column-pl-0 sorting" tabindex="0" aria-controls="datatable" rowspan="1" colspan="1" style="width: 299px;">UserName</th>
<th class="sorting" tabindex="0" aria-controls="datatable" rowspan="1" colspan="1" style="width: 195px;">DepartmentID</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.8/js/jquery.dataTables.min.js">
</script>
<script>
$(function () {
$.ajax({
type: "POST",
url: "/ApplicationUsers/LoadData",
data: '{}',
contentType: "application/json; charset=utf-8",
headers: { 'Access-Control-Allow-Origin': '*' },
dataType: "json",
success: function (response) {
OnSuccess(response);
},
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
});
// INITIALIZATION OF DATATABLES
// =======================================================
function OnSuccess(response) {
console.log(response);
$.noConflict();
$('#datatable').DataTable(
{
dom: 'Bfrtip',
bLengthChange: true,
lengthMenu: [[5, 10, -1], [5, 10, "All"]],
bFilter: true,
bSort: true,
bPaginate: true,
searching: false,
data: response,
columns: [
{ 'data': 'userID', "defaultContent": "", },
{ 'data': 'userName', "defaultContent": "", },
{ 'data': 'departmentID', "defaultContent": "", }]
});
};
</script>
public class ApplicationUsersController : Controller
{
[HttpGet]
public ActionResult Users()
{
return View();
}
[HttpPost]
public ActionResult LoadData()
{
var data = ClaimsService.GetUsers();
return Json(new { data = data});
}
}
JSON DATA
{"data":{"result":[{"userID":322,"userName":"Ashnee","departmentID":3,"branchID":1,"name":"Ashnee Pillay ","emailAddress":"Ashnee#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":323,"userName":"BalanC","departmentID":15,"branchID":5,"name":"Balan Chetty ","emailAddress":"BalanC#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":328,"userName":"BrettL","departmentID":1,"branchID":1,"name":"Brett Lange ","emailAddress":"BrettL#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":334,"userName":"CoenieB","departmentID":14,"branchID":4,"name":"Coenie De Beer ","emailAddress":"CoenieB#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":337,"userName":"TraceyA","departmentID":61,"branchID":30,"name":"Tracey De Andrade ","emailAddress":"TraceyA#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":350,"userName":"HenryB","departmentID":18,"branchID":8,"name":"Henry Barber ","emailAddress":"HenryB#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":351,"userName":"HowardG","departmentID":6,"branchID":1,"name":"Howard Gains ","emailAddress":"HowardG#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":354,"userName":"IreneK","departmentID":17,"branchID":7,"name":"Irene Koegelenberg ","emailAddress":"IreneK#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":356,"userName":"JayM","departmentID":2,"branchID":1,"name":"Jay Mahillal ","emailAddress":"JayM#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":359,"userName":"TheshniR","departmentID":4,"branchID":1,"name":"Theshni Reddy ","emailAddress":"TheshniR#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":363,"userName":"Mariov","departmentID":15,"branchID":5,"name":"Mario Vincent ","emailAddress":"mariov#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":366,"userName":"Rehana","departmentID":3,"branchID":1,"name":"Rehana Adams ","emailAddress":"Rehana#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":368,"userName":"FrancoisC","departmentID":19,"branchID":9,"name":"Francois Cloete ","emailAddress":"FrancoisC#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":369,"userName":"CharneW","departmentID":22,"branchID":10,"name":"Charne Wagner ","emailAddress":"CharneW#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":372,"userName":"Rajeshree","departmentID":3,"branchID":1,"name":"Rajeshree Singh ","emailAddress":"Rajeshree#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":377,"userName":"SamanthaM","departmentID":2,"branchID":1,"name":"Samantha Murugan ","emailAddress":"SamanthaM#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":381,"userName":"SueP","departmentID":2,"branchID":1,"name":"Sue Pillay ","emailAddress":"SueP#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":382,"userName":"Sunitha","departmentID":3,"branchID":1,"name":"Sunitha Kandhai ","emailAddress":"Sunitha#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":385,"userName":"TraceyL","departmentID":20,"branchID":12,"name":"Tracey Lambooy ","emailAddress":"TraceyL#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":387,"userName":"Vinola","departmentID":80,"branchID":30,"name":"Vinola Kalideen ","emailAddress":"Vinola#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":397,"userName":"MarioL","departmentID":5,"branchID":1,"name":"Mario Luis","emailAddress":"MarioL#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":403,"userName":"SteveJ","departmentID":9,"branchID":14,"name":"Steve Jarman","emailAddress":"SteveJ#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":404,"userName":"MarkN","departmentID":18,"branchID":1,"name":"Mark Naicker","emailAddress":"MarkN#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":406,"userName":"MosesQ","departmentID":18,"branchID":8,"name":"MosesQwabe","emailAddress":"MosesQ#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":407,"userName":"Evonn","departmentID":3,"branchID":1,"name":"Evonn Naicker","emailAddress":"Evonn#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":417,"userName":"Brandon","departmentID":18,"branchID":8,"name":"Brandon Booysen","emailAddress":"BrandonBO#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":419,"userName":"SifisoD","departmentID":9,"branchID":14,"name":"Sifiso Dlamini","emailAddress":"SifisoD#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":423,"userName":"Jnb.Workshop","departmentID":9,"branchID":14,"name":"Jnb.Workshop","emailAddress":"Jnb.Workshop#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":425,"userName":"PrenishaS","departmentID":15,"branchID":5,"name":"Prenisha Sami","emailAddress":"Prenishas#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":426,"userName":"Pzb.Operations","departmentID":20,"branchID":12,"name":"Gift Kunene","emailAddress":"Pzb.Operations#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":427,"userName":"SalomeM","departmentID":18,"branchID":8,"name":"Salome Mokwele","emailAddress":"SalomeM#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":431,"userName":"GeorgeH","departmentID":12,"branchID":3,"name":"George Horn","emailAddress":"GeorgeH#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":434,"userName":"NadineF","departmentID":22,"branchID":10,"name":"Nadine Feldtmann","emailAddress":"NadineF#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":435,"userName":"ThiroshanN","departmentID":2,"branchID":1,"name":"Thiroshan Naicker","emailAddress":"ThiroshanN#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":442,"userName":"GeneN","departmentID":14,"branchID":4,"name":"Gene Naidoo","emailAddress":"GeneN#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":444,"userName":"Jnb.Boardroom","departmentID":1,"branchID":1,"name":"Jnb.Boardroom","emailAddress":"Jnb.Boardroom#Tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":447,"userName":"Mariob","departmentID":65,"branchID":37,"name":"Mario Botha","emailAddress":"Mariob#tritonfleet.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":449,"userName":"KureshaM","departmentID":3,"branchID":1,"name":"Kuresha Moodley","emailAddress":"KureshaM#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":453,"userName":"SeanR","departmentID":3,"branchID":1,"name":"Sean Raidoo","emailAddress":"SeanR#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":459,"userName":"SalonaB","departmentID":3,"branchID":1,"name":"Salona Balram","emailAddress":"SalonaB#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":460,"userName":"JohannesS","departmentID":18,"branchID":8,"name":"Johannes Van Staden","emailAddress":"JohannesS#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":463,"userName":"Dannyg","departmentID":15,"branchID":5,"name":"Danny Govender","emailAddress":"dannyg#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":466,"userName":"Shaneilr","departmentID":5,"branchID":1,"name":"Shaneil","emailAddress":"shaneilr#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":468,"userName":"BlessingM","departmentID":19,"branchID":9,"name":"Blessing Mlimi","emailAddress":"BlessingM#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":477,"userName":"EnvorS","departmentID":13,"branchID":2,"name":"Envor Swart","emailAddress":" envors#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":481,"userName":"FritzB","departmentID":59,"branchID":25,"name":"Fritz Beudeker","emailAddress":"FritzB#tritonscs.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":483,"userName":"JuanP","departmentID":6,"branchID":1,"name":"Juan Potgieter","emailAddress":"JuanP#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":484,"userName":"NonhlanhlaMn","departmentID":18,"branchID":8,"name":"Nonhlanhla Mnguni ","emailAddress":"NonhlanhlaM#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":488,"userName":"WinnieM","departmentID":18,"branchID":8,"name":"Winnie Mbongo","emailAddress":"WinnieM#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":492,"userName":"JaendreS","departmentID":16,"branchID":6,"name":"Jaendre Serfontein","emailAddress":"JaendreS#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":497,"userName":"TshepoM","departmentID":18,"branchID":8,"name":"Tshepo Mbele","emailAddress":"TshepoM#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":498,"userName":"ItumelengL","departmentID":18,"branchID":8,"name":"Itumeleng Leso","emailAddress":"ItumelengL#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":499,"userName":"Raynoldn","departmentID":18,"branchID":8,"name":"Raynold Ngwane","emailAddress":"Raynoldn#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":504,"userName":"JarredM","departmentID":58,"branchID":24,"name":"Jarred Metzler","emailAddress":"JarredM#tritonscs.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":511,"userName":"MagdaG","departmentID":6,"branchID":1,"name":"Magda Greeff","emailAddress":"MagdaG#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":513,"userName":"WinstonB","departmentID":55,"branchID":28,"name":"Winston Blaine","emailAddress":"WinstonB#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":517,"userName":"ByronV","departmentID":27,"branchID":17,"name":"Byron Vickers ","emailAddress":"ByronV#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":521,"userName":"GertH","departmentID":61,"branchID":48,"name":"Gert Hael ","emailAddress":"GertH#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":523,"userName":"Nomsat","departmentID":88,"branchID":45,"name":"Nomsa Tshabalala","emailAddress":"NomsaT#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":527,"userName":"AndrewJ","departmentID":18,"branchID":8,"name":"Andrew Julius","emailAddress":"AndrewJ#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":528,"userName":"LeeH","departmentID":4,"branchID":1,"name":"Lee Hamberger","emailAddress":"LeeH#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":530,"userName":"KevinS","departmentID":4,"branchID":1,"name":"Kevin Smith","emailAddress":"KevinS#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":532,"userName":"PhumeleleM","departmentID":63,"branchID":33,"name":"Phumelele","emailAddress":"PhumeleleM#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":533,"userName":"GiftK","departmentID":20,"branchID":12,"name":"Gift Kunene","emailAddress":"Giftk#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":534,"userName":"MiguelF","departmentID":55,"branchID":28,"name":"Miguel","emailAddress":"MiguelF#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":535,"userName":"BilkisA","departmentID":2,"branchID":1,"name":"Bilkis Ahmod","emailAddress":"bilkisa#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":539,"userName":"HermanK","departmentID":24,"branchID":15,"name":"Herman Kleynhans","emailAddress":"HermanK#tritonexpress.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":543,"userName":"SomP","departmentID":58,"branchID":24,"name":"Som Pillay","emailAddress":"SomP#tritonscs.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"},{"userID":544,"userName":"AndriesG","departmentID":73,"branchID":42,"name":"Andries Greyling","emailAddress":"AndriesG#tritonscs.co.za","archive_User":"0","managerID":0,"isHeadOffice":0,"statusID":1,"statusDate":"0001-01-01T00:00:00"}],"id":1601,"exception":null,"status":5,"isCanceled":false,"isCompleted":true,"isCompletedSuccessfully":true,"creationOptions":0,"asyncState":null,"isFaulted":false}}
Your JSON data has the following overall structure:
{
"data": {
"result": [
{...},
{...},
{...},
...
{...}
],
"id": 1601,
"exception": null,
...
"isFaulted": false
}
}
The "result": [ ] array is where you see the data needed to populate the table. Each {...} object in that array represents one row of table data.
Therefore you have to point DataTables at that array, so it knows where, inside your JSON, to look for the data it will display. DataTables will automtically handle iteration over each item in that array.
This is why you needed to change this:
data: response,
to this:
data: response.data.result,
in your DataTable definition. Here, response is the name of the variable containing the overall response JSON. And data.result is the specific location inside that JSON where your table data is stored.

how to implement autocomplete functionality in search box in .net core mvc?

I tried to add autocomplete or suggestion functionality in search box of view, as when some one enter any character, any word containing that character shows as suggestion, but this not works. I followed different tutorials but not able to solve it. Please take a look and give me the direction.
Thnx in advance.
Controller
public async Task<IActionResult> dashboard(string sortOrder, string SearchString)
{
ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
var movies = from m in _context.Movie
select m;
if (!String.IsNullOrEmpty(SearchString))
{
movies = movies.Where(s => s.MovieName.Contains(SearchString));
}
switch (sortOrder)
{
case "name_desc":
movies = movies.OrderByDescending(s => s.MovieName);
break;
default:
movies = movies.OrderBy(s => s.MovieName);
break;
}
return View(await movies.AsNoTracking().ToListAsync());
}
public JsonResult AutoComplete(string prefix)
{
var customers = (from movie in this._context.Movie
where movie.MovieName.StartsWith(prefix)
select new
{
label = movie.MovieName,
val = movie.Id
}).ToList();
return Json(customers);
}
dashboard.cshtml
#model IEnumerable<WebApplication1.Models.Movie>
#{
ViewData["Title"] = "dashboard";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Dashboard</h1>
#using (Html.BeginForm())
{
<p>
Find by Movie Name: #Html.TextBox("SearchString")
<input type="hidden" id="hfCustomer" name="Id" />
<input type="submit" value="Search" />
</p>
}
<table class="table">
<thead>
<tr>
<th>
<a asp-action="dashboard" asp-route-sortOrder="#ViewData["NameSortParm"]">#Html.DisplayNameFor(model => model.MovieName)</a>
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.MovieName)
</td>
</tr>
}
</tbody>
</table>
<script type="text/javascript">
$(function () {
$("#txtMovie").autocomplete({
source: function (request, response) {
$.ajax({
url: '/Movies/AutoComplete/',
data: { "prefix": request.term },
type: "POST",
success: function (data) {
response($.map(data, function (item) {
return item;
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
select: function (e, i) {
$("#hfCustomer").val(i.item.val);
},
minLength: 1
});
});
</script>
I tried to add autocomplete or suggestion functionality in search box of view, as when some one enter any character, any word containing that character shows as suggestion, but this not works.
Find by Movie Name: #Html.TextBox("SearchString")
If you check the html source code of above TextBox in your browser, you would find it is rendered as below.
The value of id attribute is "SearchString", not "txtMovie". You can try to modify the code to use $("#SearchString") selector, like below.
$("#SearchString").autocomplete({
//...
//code logic here
//...
Test result with testing data
Note: please make sure you add references to required jquery libraries.
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

Getting "Not Found" when trying to retrieve data for DataTable

I´m working on asp.net WebApi with Angular doing DataTable.
View:
<table class="table table-striped table-hover table-bordered dt-bootstrap no-footer" id="tabla_catalogos" role="grid" aria-describedby="sample_editable_1_info">
<thead>
<tr>
<th class="hidden"></th>
<th style="width: 200px;"> Codigo </th>
<th> Nombre </th>
</tr>
</thead>
<tbody></tbody>
</table>
Angular control:
$scope.filtro = function (selected) {
apiService.get("../../api/Catalogo/GetCatalogoRegistro/" + selected.ID);
}
Here I get values (I recieve 5 values) but for dataTable I only need Codigo and Nombre, If I test it separately I recieve parameters without problems.
So now I use it into url parameter of datatable
Data Table:
$('#tabla_catalogos').DataTable({
searching: true,
dom: 'ftpB',
buttons: [
'excelHtml5', 'csv', 'print'
],
paging: true,
select: {
style: 'single'
},
info: false,
ordering: true,
"processing": true,
ajax: {
method: 'GET',
url: $scope.filtro = function (selected) {
apiService.get("../../api/Catalogo/GetCatalogoRegistro/" + selected.ID);
},
dataSrc: '',
beforeSend: function (request) {
request.setRequestHeader("Version", $scope.usuario.Version);
request.setRequestHeader("Origen", 'Web');
}
},
columns: [
{ data: 'Codigo' },
{ data: 'Nombre' },
],
pageLength: 10
, language: {
"emptyTable": "No se encontraron registros",
"zeroRecords": "No encontraron coincidencias",
"search": "Buscar: "
}
});
But in Chrome console, I get this error:
NOT FOUND 404:
http://localhost:55720/api/Catalogo/GetCatalogoRegistro/%22%20+%20selected.ID);%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D?_=1492444754842

What is the most likely cause of the 500 error here?

I will show you all the moving parts involved.
View:
#{
ViewBag.Title = "Partners";
}
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<h1>Partners</h1>
<p>Click to see survey answers or delete partner</p>
<table class="table">
<thead>
<tr>
<th>Partner Name</th><th>Actions</th>
</tr>
</thead>
<tbody>
#foreach ( var NameIdPair in ViewBag.PartnersAndIds )
{
<tr>
<td>
#NameIdPair.Name
</td>
<td>
<button class="btn btn-info view-partner-surveys" data-partnerid="#NameIdPair.Id">View Survey Answers</button>
<button class="btn btn-warning delete-partner" data-partnerid="#NameIdPair.Id">Delete Partner</button>
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
#section bottommost {
<script type="text/javascript">
$('.delete-partner').click(function () {
var row = $(this).closest('tr');
$.ajax({
method: 'POST',
url: 'DeletePartner',
data: { pid: $(this).attr('data-partnerid') },
dataType: 'json',
processData: false,
beforeSend: function () {
row.addClass('processing');
},
success: function (retinfo) {
if (retinfo.DeletedSuccessfully) { row.remove(); }
else { alert("Error .."); row.removeClass('processing'); }
},
error: function () { alert("Error"); row.removeClass('processing'); }
});
});
</script>
}
The problem is occuring with the AJAX call invoked with $('.delete-partner').click. The controller handling the request is the simple
[HttpPost]
public ActionResult DeletePartner ( int pid )
{
return Json(new { DeletedSuccessfully = this._Db.DeletePartner(pid) });
}
which used the method DeletePartner in a model defined by
public bool DeletePartner ( int id )
{
SqlCommand cmd = new SqlCommand("DeletePartner", this._Conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#id", id);
this._Conn.Open();
bool succeeded = cmd.ExecuteNonQuery() == 1 ? true : false;
this._Conn.Close();
return succeeded;
}
The sproc its calling is the simple
CREATE PROCEDURE DeletePartner
#id INT
AS
DELETE FROM Partners WHERE id=#id
Any idea where I'm going wrong here?
You should use the url of your ajax call like following.
url: '#Url.Action("DeletePartner")'
You have to give ajax url in the format like
url : '../controllerName/ActionName'
[HttpPost]
public ActionResult DeletePartner ( int pid )
{
return Json(new { DeletedSuccessfully = this._Db.DeletePartner(pid) });
}
The DeletedSuccessfully variable is not recognised by the controller. So it may cause 500 error

Jquery Post not reaching MVC Controller

Apologies if this i trivial, i have read many other comments and still cannot see what is wrong. I have done a few tutorials and they seem to work ok, so I am really missing something simple.
I have a basic 'remove' link that i want to do a JQuery Post back to the controller to remove an item from the database and then update the view.
My View / Javascript:
<script type="text/javascript">
$(function () {
$(".RemoveLink").click(function () {
var id = $(this).attr("data-id");
if (id != '') {
$.post("#Url.Content("~/Agent/Remove")", { "id": id }, function (data) { alert('Here i am'); });
}
});
});
#foreach (var item in Model.Object) {
<tr id="row-#item.ID">
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
<td>
<a href="#" class="RemoveLink" data-id="#item.ID" >Remove</a>
</td>
</tr>
}
My Controller:
[HttpPost]
public ActionResult Remove(int id)
{
return Json(new { Data = "true" });
}
Any assistance will be great.
Use #Url.Action("Remove", "Agent") instead.
#Url.Content("...") is used to locate any static content of the site.
Cheers
Below code works well.
#foreach (var item in Model.Object) {
<tr id="row-#item.ID">
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
<td>
<input type="button" class="RemoveLink" id="#item.ID" Value="Remove" />
</td>
</tr>
}
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$('.RemoveLink').live("click", function () {
Remove($(this));
});
});
function Remove(_this) {
var Id= $(_this).attr('id');
$.ajax({
type: 'POST',
url: '#Url.Action("Remove", "Agent")',
data: "{id: '" + Id + "'}",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data) {
//do something here......
},
error: function () {
}
});
}
</script>

Categories