I tended to copy this code from a tutorial and fix it on my database tables however i keep on getting a thrown exception which is the one mentioned above..
i tried adding data source, didnt work.
Here's my Controller method:
public ActionResult LoadData2()
{
try
{
//Creating instance of DatabaseContext class
using (AlwaysEmployedEntities _context = new AlwaysEmployedEntities())
{
var draw = Request.Form.GetValues("draw").FirstOrDefault();
var start = Request.Form.GetValues("start").FirstOrDefault();
var length = Request.Form.GetValues("length").FirstOrDefault();
var sortColumn = Request.Form.GetValues("columns[" + Request.Form.GetValues("order[0][column]").FirstOrDefault() + "][name]").FirstOrDefault();
var sortColumnDir = Request.Form.GetValues("order[0][dir]").FirstOrDefault();
var searchValue = Request.Form.GetValues("search[value]").FirstOrDefault();
//Paging Size (10,20,50,100)
int pageSize = length != null ? Convert.ToInt32(length) : 0;
int skip = start != null ? Convert.ToInt32(start) : 0;
int recordsTotal = 0;
// Getting all Customer data
var customerData = (from tempcustomer in _context.AspNetUsers
select tempcustomer);
//Sorting
if (!(string.IsNullOrEmpty(sortColumn) && string.IsNullOrEmpty(sortColumnDir)))
{
customerData = customerData.OrderBy(sortColumn + " " + sortColumnDir);
}
//Search
if (!string.IsNullOrEmpty(searchValue))
{
customerData = customerData.Where(m => m.UserName == searchValue );
}
//total number of rows count
recordsTotal = customerData.Count();
//Paging
var data = customerData.Skip(skip).Take(pageSize).ToList();
//Returning Json Data
return Json(new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = data });
}
}
catch (Exception)
{
throw;
}
}
And here's my View:
@{
Layout = null;
var id = Url.RequestContext.RouteData.Values["id"];
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Browse Users</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<link href="https://cdn.datatables.net/1.10.15/css/dataTables.bootstrap.min.css" rel="stylesheet" />
<link href="https://cdn.datatables.net/responsive/2.1.1/css/responsive.bootstrap.min.css" rel="stylesheet" />
<script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.15/js/dataTables.bootstrap4.min.js "></script>
<script>
$(document).ready(function () {
$("#myTable").DataTable({
"processing": false, // for show progress bar
"serverSide": true, // for process server side
"filter": false, // this is for disable filter (search box)
"orderMulti": true, // for disable multiple column at once
"pageLength": 10,
"ajax": {
"url": "/Browse/LoadData2",
"type": "POST",
"datatype": "json"
},
"columns": [
{ "data": "Id", "name": "Id", "autoWidth": true },
{ "data": "UserName", "name": "UserName", "autoWidth": true }
]
});
});
</script>
<link href="/HackIt/css/style.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css">
<style type="text/css">
#header {
padding: 10px 0;
height: 50px;
position: fixed;
left: 0;
background: rgba(0, 0, 0, 0.8);
top: 0;
right: 0;
transition: all 0.5s;
z-index: 997;
}
body {
background-image: url('/Login_v3/images/about2.jpeg');
background-size: cover;
background-attachment: fixed;
font-family: 'Open Sans', Arial, Helvetica, Sans-serif, Verdana, Tahoma;
}
label {
display: inline-block;
max-width: 100%;
margin-bottom: 5px;
color: black;
font-weight: 700;
}
.table > thead > tr > th, .table > tbody > tr > th, .table > tfoot > tr > th, .table > thead > tr > td, .table > tbody > tr > td, .table > tfoot > tr > td {
padding: 8px;
line-height: 1.42857143;
vertical-align: top;
border-top: 3px solid #ddd;
}
@@-webkit-keyframes zoomIn {
from {
opacity: 0;
-webkit-transform: scale3d(0.3, 0.3, 0.3);
transform: scale3d(0.3, 0.3, 0.3);
}
50% {
opacity: 1;
}
}
.zoomIn {
-webkit-animation-name: zoomIn;
animation-name: zoomIn;
}
@@keyframes zoomIn {
from {
opacity: 0;
-webkit-transform: scale3d(0.3, 0.3, 0.3);
transform: scale3d(0.3, 0.3, 0.3);
}
50% {
opacity: 1;
}
}
@@media only screen and (min-width: 350px) and (max-width: 1030px) {
.container {
margin-top: 20%;
}
}
table.dataTable tbody tr {
background: rgba(0, 0, 0, 0.8);
}
.table-striped > tbody > tr:nth-child(odd) > td, .table-striped > tbody > tr:nth-child(odd) > th {
background-color: rgba(0, 0, 0, 0.8);
}
.dataTables_wrapper .dataTables_paginate .paginate_button.disabled, .dataTables_wrapper .dataTables_paginate .paginate_button.disabled:hover, .dataTables_wrapper .dataTables_paginate .paginate_button.disabled:active {
cursor: default;
color: black !important;
border: 1px solid transparent;
background: transparent;
box-shadow: none;
font-weight: 700;
}
@@media only screen and (min-width: 1030px) {
.container {
margin-top: 5%;
}
}
#myTable_info {
color: black;
font-weight: 700;
}
th {
width: 200px;
}
tbody {
color: aliceblue;
}
</style>
</head><body style="margin:20px auto">
<main id="main">
<header id="header">
<div class="container-fluid">
<div id="logo" class="pull-left">
<h1><a href="@Url.Action("Index", "Home", new { id = id
})" class="scrollto" style="text-decoration:none;font-size:25px;"></a></h1>
</div>
</div>
</header>
</main>
<div class="container">
<div class="row heade/r">
<center>
<h2>Emp</h2>
</center>
<a href="@Url.Action("Browse", "Browse", new { id = id })"></a>
</div>
<table id="myTable" class="table table-striped"">
<thead>
<tr>
<th><center>Id</center></th>
<th><center>UserName</center></th>
</tr>
</thead>
</table>
</div>
</div>
I cant relate whats the data source issue or the problem.. my model is functioning normally on other pages.. Any help would be appreciated.. thanks Any help would be appreciated , thanks!