【问题标题】:jquery datatable in visual studio plugin not workingVisual Studio插件中的jquery数据表不起作用
【发布时间】:2021-01-12 13:32:22
【问题描述】:

嘿,我试图在 Visual Studio 2019 中为我的代码创建一个 jquery 数据表,但数据表不会显示我已经成功安装了“Install-Package bootbox”和“Install-Package jQuery.DataTables”,但是当我运行代码时数据表不显示。我浏览了整个网络都找不到答案。任何帮助将不胜感激。

编辑:当我右键单击项目“检查”时,会出现此错误:

jquery.dataTables.js:1197 Uncaught TypeError: Cannot read property 'mData' of undefined
    at HTMLTableCellElement.<anonymous> (jquery.dataTables.js:1197)
    at Function.each (jquery-3.4.1.js:367)
    at jQuery.fn.init.each (jquery-3.4.1.js:202)
    at HTMLTableElement.<anonymous> (jquery.dataTables.js:1194)
    at Function.each (jquery-3.4.1.js:367)
    at jQuery.fn.init.each (jquery-3.4.1.js:202)
    at jQuery.fn.init.DataTable [as dataTable] (jquery.dataTables.js:869)
    at jQuery.fn.init.$.fn.DataTable (jquery.dataTables.js:15172)
    at Index:153

BundleConfig.cs

using System.Web;
using System.Web.Optimization;

namespace AuditoriskaMVC1
{
    public class BundleConfig
    {
        // For more information on bundling, visit https://go.microsoft.com/fwlink/?LinkId=301862
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.validate*"));

            // Use the development version of Modernizr to develop with and learn from. Then, when you're
            // ready for production, use the build tool at https://modernizr.com to pick only the tests you need.
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                      "~/Scripts/bootstrap.js",
                      "~/Scripts/bootbox.js",
                      "~/Scripts/DataTables/jquery.dataTables.js",
                      "~/Scripts/DataTables/dataTables.bootstrap.js"
                    ));

            bundles.Add(new StyleBundle("~/Content/css").Include(
                      "~/Content/bootstrap.css",
                      "~/Content/site.css",
                      "~/Content/DataTables/css/dataTables.bootstrap.css"));
        }
    }
}

索引.html

@model IEnumerable<AuditoriskaMVC1.Models.Movie>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table" id="moviesTable">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.DownloadURL)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ImagedURL)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Rating)
            </th>
        
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    <a href="@item.DownloadURL">Download Here!</a>

                </td>
                <td>
                    <img style="max-width:200px" src="@item.ImagedURL" />

                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Rating)

                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                    @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.Id })
                </td>
            </tr>
        }
    </tbody>
</table>

@section  scripts{
     <script>
         $("#moviesTable").DataTable();
     </script>   
    
}

【问题讨论】:

    标签: jquery visual-studio datatables


    【解决方案1】:

    好吧,我想通了,但以防万一其他人有这个问题,我会留下这篇文章。

    解决办法是表头和表体要等量

    在这种情况下,表头少了 1 个,所以即使您在 Visual Studio 中由于某种原因没有收到任何错误,背景中也有一个错误,您只能在右键单击并检查您的页面时看到它。如果您的头部或身体中的它们较少,则修复只是添加一个空白。

    【讨论】:

    • 合理的数量,抱歉?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多