User388869887 posted
I am writing a code to Read companies details. I am using datatables. The problem is i am able to see the data in xhr using developer tools in the dataset. but it does not show up on the page. The page says there is no data in the table. While i have
data stored in that table.
Here is my code.
@{
ViewBag.Title = "View";
Layout = "~/Views/Shared/BackEnd/_LayoutBackEnd.cshtml";
}
@section HeadAssets{
<script type="text/javascript" src="~/Assets/BackEnd/js/plugins/jquery/jquery.min.js"></script>
<link href="~/Assets/BackEnd/css/jquery/datatables.min.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="~/Assets/BackEnd/js/plugins/datatables/jquery.dataTables.min.js"></script>}
@section NavBar{
}
@section LeftContainer{
}
@section AdminBody{
<div class="content">
<table id="myDbTable">
<thead>
<tr>
<th >ID</th>
<th>Name</th>
<th>E-mail</th>
<th>Phone</th>
</tr>
</thead>
</table>
</div>
}
@section Scripts{
<script>
$(document).ready(function () {
$.ajax({
url: '@Url.Action("GetCompanies", "Company")',
method: 'get',
dataType: 'json',
success: function (data) {
$('#myDbTable').dataTable({
data: data,
columns: [
{ "data": "Id" },
{ "data": "Name" },
{ "data": "Email" },
{ "data": "Owner" },
]
});
}
});
});
</script>
}
Here is my controller code:
[HttpGet]
public JsonResult GetCompanies()
{
try
{
IEnumerable<Company> company = _companyService.GetCompanies().ToList();
IEnumerable<CompanyListViewModel> viewModelListCompanies = Mapper.DynamicMap<IEnumerable<Company>, IEnumerable<CompanyListViewModel>>(company);
return new JsonSuccessResult(viewModelListCompanies);
//return Json(accountTypes, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
Response.StatusCode = (int)ResponseCode.UnprocessableEntity;
return new JsonErrorResult(ex.ToString());
}
}
Can any body help me please. Any help in this regard will be highly appreciated.