0

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;
    }

}
7
  • What exactly is a 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 like data instead of just returning the raw array, you will need to adjust the JS accordingly. Commented Jun 30, 2017 at 16:48
  • JsonSuccessResult is a class which uses enums and dictionaries to return success message if the requested operation is completed successfully. Commented Jun 30, 2017 at 16:52
  • So what does the returned JSON look like? Commented Jun 30, 2017 at 16:54
  • I have updated the code with that class as well Commented Jun 30, 2017 at 16:56
  • returned Json Dataset is as follows: DataSet : [{Id: 1, Name: "imran", Owner: "sd", Email: "[email protected]"}] 0 : {Id: 1, Name: "imran", Owner: "sd", Email: "[email protected]"} Email : "[email protected]" Id : 1 Name : "imran" Owner : "sd" Error : false Limit : 50 Message : "Success" Offset : 0 Success : true TotalCount : 0 Commented Jun 30, 2017 at 16:57

1 Answer 1

1

Your returned JsonSuccessResult object isn't an array, it's an object that has the data in a property called DataSet. So you need to change

success: function (data) {
    $('#DbTable').DataTable({
        data: data,

to

success: function (data) {
    $('#DbTable').DataTable({
        data: data.DataSet,
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.