_Layout.cshtml和我的ViewFile.cshtml之间有什么相互作用?

时间:2018-11-11 17:04:20

标签: jquery asp.net-mvc datatables

在_Layout.cshtml中,有对jQuery和bootstrap.min.js的引用。 但是,在ViewFile页面上,除非包含对jQuery和数据表的引用,否则我无法使DataTables正常工作。这是在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.

1 个答案:

答案 0 :(得分:1)

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

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

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