I am using a datatables to get data from database. The problem i am facing is that the controller method to get data is working properly and returning DataSet which i can see in xhr but the data is not showing in html page. I can't figure out the problem. Any help in this regard will be appreciated. Here is my code snippet.
<table id="DbTable">
<thead>
<tr>
<th >ID</th>
<th>Name</th>
<th>E-mail</th>
<th>Address</th>
</tr>
</thead>
</table>
Script code:
$(document).ready(function () {
$.ajax({
url: '@Url.Action("GetCompanies", "Company")',
method: 'get',
dataType: 'json',
success: function (data) {
$('#DbTable').DataTable({
data: data,
columns: [
{ "data": "Id" },
{ "data": "Name" },
{ "data": "Email" },
{ "data": "Address" },
]
});
}
});
});
Here is the 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);
}
catch (Exception ex)
{
Response.StatusCode = (int)ResponseCode.UnprocessableEntity;
return new JsonErrorResult(ex.ToString());
}
}
JsonSuccessResult Class:
public class JsonSuccessResult : JsonResult
{
public JsonSuccessResult()
{
Data = new { Success = true, Error = false, Message = "Data has been saved successfully" };
ContentEncoding = System.Text.Encoding.UTF8;
JsonRequestBehavior = JsonRequestBehavior.AllowGet;
}
public JsonSuccessResult(MessageCode messageCode)
{
Data = new { Success = true, Error = false, Message = MessageDictionary.SuccessMessages[messageCode] };
ContentEncoding = System.Text.Encoding.UTF8;
JsonRequestBehavior = JsonRequestBehavior.AllowGet;
}
public JsonSuccessResult(string message)
{
Data = new { Success = true, Error = false, Message = message };
ContentEncoding = System.Text.Encoding.UTF8;
JsonRequestBehavior = JsonRequestBehavior.AllowGet;
}
public JsonSuccessResult(IEnumerable enumerable, long totalCount = 0, int limit = 50, int offset = 0)
{
Data = new
{
Success = true,
Error = false,
Message = "Success",
DataSet = enumerable,
Limit = limit,
Offset = offset,
TotalCount = totalCount,
};
ContentEncoding = System.Text.Encoding.UTF8;
JsonRequestBehavior = JsonRequestBehavior.AllowGet;
}
public JsonSuccessResult(object data)
{
Data = new
{
Success = true,
Error = false,
Message = "Success",
Data = data
};
ContentEncoding = System.Text.Encoding.UTF8;
JsonRequestBehavior = JsonRequestBehavior.AllowGet;
}
public JsonSuccessResult(int val)
{
Data = new
{
Success = true,
Error = false,
Message = "Success",
Data = val
};
ContentEncoding = System.Text.Encoding.UTF8;
JsonRequestBehavior = JsonRequestBehavior.AllowGet;
}
}
JsonSuccessResult? I searched for it and only see it in other SO questions with code very similar to yours. If it puts the results in a property likedatainstead of just returning the raw array, you will need to adjust the JS accordingly.