c# – How to return the View with JSON result?

Is there any way to return the View with JSON result? I’ve done like this but it returns me a json results only I want to bind json result with jQuery Datatable.

this is my controller:

[HttpGet]
    public async Task <IActionResult> GetDepartments()
    {
        try
        {
            ...
            var result = await _get.GetRequest<string>(uri, accessToken);
            return Json(result);
            }
        }

        catch (Exception ex)
        {
           ..
        }

For my View I’m doing like this:

<table id="myDataTable">
    <thead>
        <tr>
            <td>Id</td>
            <td>Name</td>
            <td>Department</td>
            <td>Manager</td>

        </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>
        $(document).ready(function () {
            $('#myDataTable').DataTable({
                ajax: {
                    url: '/Home/GetDepartments',
                    "dataSrc": ""
                },
                columns: [
                    { data: "id" },
                    { data: "name" },
                    { data: "department" },
                    { data: "manager" }
                ]
            });
        });


</script>

That’s what I got

[{"id":3,"name":"Sales","department":null,"manager":"Danial Booker"},{"id":4,"name":"PMO","department":null,"manager":"Rowan Walter"},{"id":5,"name":"Research And Development","department":null,"manager":"Shani Elliott"},{"id":6,"name":"Product Management","department":null,"manager":"Menna Goff"},{"id":7,"name":"HR","department":null,"manager":"Jayda Martinez"},{"id":8,"name":"Deve","department":"Product Management","manager":"Abigayle Briggs"},{"id":9,"name":"Test","department":"Product Management","manager":"Alys Huang"},{"id":19,"name":"QA","department":"Ava","manager":"Mela "}]

I want to show the view that have the Plugin jQuery datatable with json result, any help please?

Read more here: Source link