_Layout.cshtml和我的ViewFile.cshtml之间的交互是什么?

问题描述 投票:0回答:1

在_Layout.cshtml中有对jQuery和bootstrap.min.js的引用。然而,在我的ViewFile页面中,除非我包含对jQuery和数据表的引用,否则我无法使DataTable工作。这是在Visual Studio 2017 for Mac中。我正在尝试获取一个分页表,响应浏览器调整大小,具有列排序,搜索功能,第一个,下一个等漂亮的按钮,左上角的页面大小选择器和右上角的搜索窗口。我不断得到不同的组合,当它看起来正确时,没有列排序按钮等。这是_Layout.cshtml文件中的内容:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - Maestro</title>
    <environment include="Development">
        <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
        <link rel="stylesheet" href="~/css/site.css" />
    </environment>
    <environment exclude="Development">
        <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css"
              asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
              asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
        <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
    </environment>
</head>
<body>
    <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a asp-area="" asp-controller="LogFileViewer" asp-action="SelectFile" class="navbar-brand">Maestro</a>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li><a asp-area="" asp-controller="LogFileViewer" asp-action="SelectFile">LogFiles</a></li>
                    <li><a asp-area="" asp-controller="Home" asp-action="About">About</a></li>
                    <li><a asp-area="" asp-controller="Home" asp-action="Contact">Contact</a></li>
                </ul>
            </div>
        </div>
    </nav>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; 2018 - Maestro</p>
        </footer>
    </div>
    <environment include="Development">
        <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
        <script src="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/bootstrap.min.js"></script>
    </environment>
    <environment exclude="Development">
        <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.3.1.min.js"
                asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
                asp-fallback-test="window.jQuery"
                crossorigin="anonymous"
                integrity="sha384-tsQFqpEReu7ZLhBV2VZlAu7zcOV+rXbYlF2cqB8txI/8aZajjp4Bqd+V6D5IgvKT"></script>
        <script src="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/bootstrap.min.js"
                asp-fallback-src="~/lib/bootstrap/dist/js/bootstrap.min.js"
                asp-fallback-test="window.jQuery && window.jQuery.fn && window.jQuery.fn.modal"
                crossorigin="anonymous"
                integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"></script>
        <script src="~/js/site.min.js" asp-append-version="true"></script>
    </environment>
    @RenderSection("Scripts", required: false)
</body>
</html>

在我的ViewFile.cshtml的顶部:

@{
    ViewData["Title"] = "View";
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
    <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
    <script src="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/bootstrap.min.js"></script>
}

表定义为:

  <table id="table_id" class="table table-condensed table-striped table-hover" width="100%" cellspacing="0">

在桌子下面是:

<script>$(document).ready(function ()
    {
    $('#table_id').dataTable( {
                "pagingType": "full_numbers",
                "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
                "scrollY"  : "400px",
                "paging": true,
                "ordering": true,
                "info": true,
                "searching": true,
                "processing": true, // for show progress bar
                "serverSide": false, // for process server side
                "filter": true, // this is for disable filter (search box)
                "orderMulti": false, // for disable multiple column at once
                "columnDefs":
                [{
                    "targets": [0],
                    "visible": false,
                    "searchable": false
                }],
                 "columns": [
                    { "data": "DateTime", "name": "DateTime", "autoWidth": true },
                    { "data": "Msecs", "name": "Msecs", "autoWidth": true },
                    { "data": "Thread", "name": "Thread", "autoWidth": true },
                    { "data": "Level", "name": "Level", "autoWidth": true },
                    { "data": "Logger", "name": "Logger", "autoWidth": true },
                    { "data": "Host", "name": "Host", "autoWidth": true },
                    { "data": "MsgType", "name": "MsgType", "autoWidth": true },
                    { "data": "Message", "name": "Message", "autoWidth": true }
                ]

            });
        });</script>

> Is there some interaction between the js and css files in the
> _Layout.cshtml and the ones at the top of the ViewFile.cshtml?
> 
> What would be THE set of js and css files to include to get what I
> want?  I can't find images of what a set does.
jquery asp.net-mvc datatables
1个回答
1
投票

在ViewFile.cshtml中的@section scripts {}中声明您的数据表javascript代码

@section scripts {
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
}

并在该部分中放置您的其他JavaScript代码(用于数据表初始化)

© www.soinside.com 2019 - 2024. All rights reserved.