Hello guys, i have a big problem, i try to integrate datatables with asp.net core app

Hello guys, i have a big problem, i try to integrate datatables with asp.net core app

zamfirbeniaminzamfirbeniamin Posts: 1Questions: 1Answers: 0
edited July 2020 in Free community support
Layout file:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - BoolListRazor</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" />
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css" />
</head>
<body>
    <header>
        <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
            <div class="container">
                <a class="navbar-brand" asp-area="" asp-page="/Index">BoolListRazor</a>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
                    <ul class="navbar-nav flex-grow-1">
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-page="/BookList/Index">Book</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
    </header>
    <div class="container">
        <main role="main" class="pb-3">
            @RenderBody()
        </main>
    </div>

    <footer class="border-top footer text-muted">
        <div class="container">
            &copy; 2020 - BoolListRazor - <a asp-area="" asp-page="/Privacy">Privacy</a>
        </div>
    </footer>

    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>
    <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
    <script type="text/javascript" src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>



    @RenderSection("Scripts", required: false)
</body>
</html>

cshtml file:
@page
@model BoolListRazor.IndexModel


<br />
<div class="container row p-0 m-0"><!--p - padding, m - margin-->
    <div class="col-9">
        <h2 class="text-info">Book List</h2>
    </div>
    <div class="col-3">
        <a asp-page ="Create" class=" btn btn-info form-control text-white">Create New Book</a>
    </div>

    <div class="col-12 border p-3 mt-3">
        <form method="post">
            @if(Model.Books.Count() > 0)
            {
                <table class="table table-striped border">
                    <tr class="table-secondary">
                        <th>
                            <label asp-for="Books.FirstOrDefault().Name"></label>
                        </th>
                        <th>
                            @*@Html.DisplayNameFor(m=>m.Books.FirstOrDefault().Author)*@
                            <label asp-for="Books.FirstOrDefault().Author"></label>
                        </th>
                        <th>
                            <label asp-for="Books.FirstOrDefault().ISBN"></label>
                        </th>
                        <th>

                        </th>
                    </tr>
                    @foreach(var item in Model.Books)
                    {
                <tr>
                    <td>
                        @Html.DisplayFor(m => item.Name)
                    </td>
                    <td>
                        @Html.DisplayFor(m => item.Author)
                    </td>
                    <td>
                        @Html.DisplayFor(m => item.ISBN)
                    </td>
                    <td>
                        <button asp-page-handler ="Delete" 
                                asp-route-id="@item.Id"  
                                onclick="return confirm('Are you sure you want to delete it?')" 
                                class="btn btn-danger btn-sm">
                            Delete
                        </button>
                        <a asp-page ="Edit" asp-route-id="@item.Id" class="btn btn-success btn-sm text-white">Edit</a>
                    </td>
                </tr>
                    }
                </table>
            }
            else
            {
                <p> No books available</p>
            }
        </form>
    </div>


    <div class="col-12" style="text-align:center">
        <br/>
        <span class="h3 text-info">OR</span>
        <br/>
    </div>

    <div class="col-12 border p-3">
        <table id="DT_load" class="table table-striped table-bordered" style="width:100%">
            <thead>
                <tr>
                    <th>Name</th>
                    <th> Author</th>
                    <th> ISBN</th>
                    <th></th>
                </tr>
            </thead>
        </table>
    </div>
</div>
@section Scripts{ 
    <script src="~/js/bookList.js"></script>
}

js file:
var dataTable;

$(document).ready(function () {
    loadDataTable();
});

function loadDataTable() {
    dataTable = $('#DT_load').DateTable({
        "ajax": {
            "url": "/api/book",
            "type": "GET",
            "datatype": "json"
        },
        "columns": [
            { "data": "name", "width": "30%" },
            { "data": "author", "width": "30%" },
            { "data": "isbn", "width": "30%" },
            {
                "data": "id",
                "render": function (data) {
                    return `<div class="text-center">
                                <a href="/BookList/Edit?id=${data}" class='btn btn-success text-white' style='cursor:pointer; width:100px;'>
                                    Edit
                                </a>
                                &nbsp;
                                <a class='btn btn-danger text-white' style='cursor:pointer; width:100px;'>
                                    Delete
                                </a>
                            </div>`;
                }, "width": "30%"
            }
        ],
        "language": {
            "emptyTable": "no data found"
        },
        "width": "100%"
    });
}

c# api controller file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BoolListRazor.Model;
using Microsoft.AspNetCore.Mvc;

namespace BoolListRazor.Controllers
{
    [Route("api/Book")]
    [ApiController]
    public class BookController : Controller
    {
        private readonly ApplicationDbContext _db;

        public BookController(ApplicationDbContext db)
        {
            _db = db;
        }

        [HttpGet]
        public IActionResult GetAll()
        {
            return Json(new { data = _db.Book.ToList() });
        }
    }
}
This discussion has been closed.