jQuery DataTableS样式在ASP Net Core Razor页面中不起作用的部分视图

发布于 2025-02-11 06:30:59 字数 5304 浏览 0 评论 0原文

我有一个部分视图,该视图具有数据列表以显示客户列表。一切正常,除了DataTableS未被设计,并且未显示搜索。我添加了所有参考文献,并在

_layout.cshtml


    <html>
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
        <meta name="description" content="" />
        <meta name="author" content="" />
        <title>@ViewBag.Title</title>
        <link href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css" rel="stylesheet"/>
    </head>
    <body class="sb-nav-fixed">
        @RenderBody()
    <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>   
    <script src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js"></script>
    </body>
    </html>

custormers.cshtml

@model Web.Pages.Customer.CustomersModel
@{
    ViewData["Title"] = "Registration";
}
<div class="card">
    <div class="col-sm-12" style="padding:20px">
        <a onclick="jQueryModalGet('?handler=CreateOrEdit','New Customer')" class="btn btn-success">
            Create
        </a>
        
        <a id="reload" class="btn bg-warning">
            Reload
        </a>
    </div>
    <div id="viewAll" class="card-body table-responsive"></div>
</div>

@section Scripts
{
    <script>
        $(document).ready(function () {
            $('#viewAll').load('?handler=ViewAllPartial');
        });
        $(function () {
            $('#reload').on('click', function () {
                $('#viewAll').load('?handler=ViewAllPartial');
            });
        });
    </script>
}

customersmodel.cs

{
    public class CustomersModel : PageModel
    {
        private readonly ICustomerRepository _customer;
        private readonly IUnitOfWork _unitOfWork;
        private readonly IRazorRenderService _renderService;
        private readonly ILogger<CustomersModel> _logger;

        public CustomersModel(ILogger<CustomersModel> logger, ICustomerRepository customer, IRazorRenderService renderService)
        {
            _logger = logger;
            _customer = customer;
            _renderService = renderService;
        }
        public IEnumerable<Core.Entities.Customer> Customers { get; set; }
        public IEnumerable<CustomerModel> CustomerModel{ get; set; }
        
        public void OnGet()
        {
        }

        public PartialViewResult OnGetViewAllPartial()
        {
            CustomerModel = _customer.GetAsync();
            return new PartialViewResult
            {
                ViewName = "_ViewAll",
                ViewData = new ViewDataDictionary<IEnumerable<CustomerModel>>(ViewData, CustomerModel)
            };
        }
    }
}

_viewall.cshtml

@model IEnumerable<CustomerModel>


<table class="display" id="#dataTable">
    <thead>
        <tr>
            <th>
                FirstName
            </th>
            <th>
                LastName
            </th>
            <th>
                Age
            </th>
            <th>
                PhoneNumber
            </th>
            <th>
                Address
            </th>
        </tr>
    </thead>
    <tbody>
        @if (Model.Count() != 0)
        {
            @foreach (var customer in Model)
            {
                <tr>
                    <td>
                        @customer.FirstName
                    </td>
                    <td>
                        @customer.LastName
                    </td>
                    <td>
                        @customer.Age
                    </td>
                    <td>
                        @customer.PhoneNumber
                    </td>
                    <td>
                        @customer.Address
                    </td>
                </tr>
            }
        }
    </tbody>
</table>
<script>
    $(document).ready(function () {
        $("#dataTable").DataTable();
    });
</script>

BWROSER

I have a partial view that has a datatable to show list of customers. Everything works fine except that datatables is not styled and search is not shown. I have added all the references and tried it on This JS Fiddle and it works fine. But when it comes to my razor pages it doesn't.I have included screenshot of my out pit at the end.Below is my code.

_Layout.cshtml


    <html>
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
        <meta name="description" content="" />
        <meta name="author" content="" />
        <title>@ViewBag.Title</title>
        <link href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css" rel="stylesheet"/>
    </head>
    <body class="sb-nav-fixed">
        @RenderBody()
    <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>   
    <script src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js"></script>
    </body>
    </html>

Custormers.cshtml

@model Web.Pages.Customer.CustomersModel
@{
    ViewData["Title"] = "Registration";
}
<div class="card">
    <div class="col-sm-12" style="padding:20px">
        <a onclick="jQueryModalGet('?handler=CreateOrEdit','New Customer')" class="btn btn-success">
            Create
        </a>
        
        <a id="reload" class="btn bg-warning">
            Reload
        </a>
    </div>
    <div id="viewAll" class="card-body table-responsive"></div>
</div>

@section Scripts
{
    <script>
        $(document).ready(function () {
            $('#viewAll').load('?handler=ViewAllPartial');
        });
        $(function () {
            $('#reload').on('click', function () {
                $('#viewAll').load('?handler=ViewAllPartial');
            });
        });
    </script>
}

CustomersModel.cs

{
    public class CustomersModel : PageModel
    {
        private readonly ICustomerRepository _customer;
        private readonly IUnitOfWork _unitOfWork;
        private readonly IRazorRenderService _renderService;
        private readonly ILogger<CustomersModel> _logger;

        public CustomersModel(ILogger<CustomersModel> logger, ICustomerRepository customer, IRazorRenderService renderService)
        {
            _logger = logger;
            _customer = customer;
            _renderService = renderService;
        }
        public IEnumerable<Core.Entities.Customer> Customers { get; set; }
        public IEnumerable<CustomerModel> CustomerModel{ get; set; }
        
        public void OnGet()
        {
        }

        public PartialViewResult OnGetViewAllPartial()
        {
            CustomerModel = _customer.GetAsync();
            return new PartialViewResult
            {
                ViewName = "_ViewAll",
                ViewData = new ViewDataDictionary<IEnumerable<CustomerModel>>(ViewData, CustomerModel)
            };
        }
    }
}

_ViewAll.cshtml

@model IEnumerable<CustomerModel>


<table class="display" id="#dataTable">
    <thead>
        <tr>
            <th>
                FirstName
            </th>
            <th>
                LastName
            </th>
            <th>
                Age
            </th>
            <th>
                PhoneNumber
            </th>
            <th>
                Address
            </th>
        </tr>
    </thead>
    <tbody>
        @if (Model.Count() != 0)
        {
            @foreach (var customer in Model)
            {
                <tr>
                    <td>
                        @customer.FirstName
                    </td>
                    <td>
                        @customer.LastName
                    </td>
                    <td>
                        @customer.Age
                    </td>
                    <td>
                        @customer.PhoneNumber
                    </td>
                    <td>
                        @customer.Address
                    </td>
                </tr>
            }
        }
    </tbody>
</table>
<script>
    $(document).ready(function () {
        $("#dataTable").DataTable();
    });
</script>

Update Network tab of the Bwroser
CSS loaded fine
jQuery Loaded fine
Datatables loaded fine
Output Image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘逸的'云 2025-02-18 06:30:59

首先,您需要将id =“#DataTable”更改为 id =“ datatable” 。代码> datatables.min.js 和JS代码中的castormers.cshtml。

_viewall.cshtml:

@model IEnumerable<CustomerModel>


<table class="display" id="dataTable">
    <thead>
        <tr>
            <th>
                FirstName
            </th>
            <th>
                LastName
            </th>
            <th>
                Age
            </th>
            <th>
                PhoneNumber
            </th>
            <th>
                Address
            </th>
        </tr>
    </thead>
    <tbody>
        @if (Model.Count() != 0)
        {
            @foreach (var customer in Model)
            {
                <tr>
                    <td>
                        @customer.FirstName
                    </td>
                    <td>
                        @customer.LastName
                    </td>
                    <td>
                        @customer.Age
                    </td>
                    <td>
                        @customer.PhoneNumber
                    </td>
                    <td>
                        @customer.Address
                    </td>
                </tr>
            }
        }
    </tbody>
</table>

custormers.cshtml:

@model Web.Pages.Customer.CustomersModel
@{
    ViewData["Title"] = "Registration";
}
<div class="card">
    <div class="col-sm-12" style="padding:20px">
        <a onclick="jQueryModalGet('?handler=CreateOrEdit','New Customer')" class="btn btn-success">
            Create
        </a>
        
        <a id="reload" class="btn bg-warning">
            Reload
        </a>
    </div>
    <div id="viewAll" class="card-body table-responsive"></div>
</div>

@section Scripts
{
    <link href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css" rel="stylesheet"/>
    <script src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js"></script>
    <script>
        $(function () {
            $('#viewAll').load('?handler=ViewAllPartial', function () {
                $("#dataTable").DataTable();
            });
        });
    </script>
}

结果:

Firstly,you need to change id="#dataTable" to id="dataTable".And then put dataTables.min.css,dataTables.min.js and js code into Custormers.cshtml.

_ViewAll.cshtml:

@model IEnumerable<CustomerModel>


<table class="display" id="dataTable">
    <thead>
        <tr>
            <th>
                FirstName
            </th>
            <th>
                LastName
            </th>
            <th>
                Age
            </th>
            <th>
                PhoneNumber
            </th>
            <th>
                Address
            </th>
        </tr>
    </thead>
    <tbody>
        @if (Model.Count() != 0)
        {
            @foreach (var customer in Model)
            {
                <tr>
                    <td>
                        @customer.FirstName
                    </td>
                    <td>
                        @customer.LastName
                    </td>
                    <td>
                        @customer.Age
                    </td>
                    <td>
                        @customer.PhoneNumber
                    </td>
                    <td>
                        @customer.Address
                    </td>
                </tr>
            }
        }
    </tbody>
</table>

Custormers.cshtml:

@model Web.Pages.Customer.CustomersModel
@{
    ViewData["Title"] = "Registration";
}
<div class="card">
    <div class="col-sm-12" style="padding:20px">
        <a onclick="jQueryModalGet('?handler=CreateOrEdit','New Customer')" class="btn btn-success">
            Create
        </a>
        
        <a id="reload" class="btn bg-warning">
            Reload
        </a>
    </div>
    <div id="viewAll" class="card-body table-responsive"></div>
</div>

@section Scripts
{
    <link href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css" rel="stylesheet"/>
    <script src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js"></script>
    <script>
        $(function () {
            $('#viewAll').load('?handler=ViewAllPartial', function () {
                $("#dataTable").DataTable();
            });
        });
    </script>
}

result:
enter image description here

妄司 2025-02-18 06:30:59

如果缺少样式,则CSS也无法正确加载,并且对于DataTable.js文件是搜索栏丢失。请检查您的代码并发送捕获,检查检查员控制台上的任何错误并共享反馈。

If the style is missing the the CSS is not loading correctly, as well for the datatable.js file is the search bar is missing. Please inspect your code and send a capture, check for any error on inspector console and share the feedback.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文