向SOers求助
我目前正在创建一个ASP.NET MVC项目,并且由于将表包装在jQuery DataTables中而发脾气。为了进行测试,我运行了附带数据库的标准MVC模板(DB First方法)。它成功地用数据填充了表,但没有提供jQuery DataTables功能(甚至是基本功能)。来自另一个数据库表(WeaponTypes中的WeaponTypeDescr列)的数据也已成功上传到该表中。控制器和视图都是通过ItemType模型的脚手架选项创建的,该表应在“索引”页面中运行。 这是ItemTypes / Index的代码:
@model IEnumerable<WebApplication1.Models.ItemType>
@{
ViewBag.Title = "Index";
}
@section styles {
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.18/datatables.min.css" />
}
@section scripts {
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function () {
$('#ItemTypeTable').DataTable(
);
});
</script>
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table id="ItemTypeTable">
<tr>
<th>
@Html.DisplayNameFor(model => model.ItemType1)
</th>
<th>
@Html.DisplayNameFor(model => model.WeaponType.WeaponTypeDescr)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ItemType1)
</td>
<td>
@Html.DisplayFor(modelItem => item.WeaponType.WeaponTypeDescr)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.ItemTypeID }) |
@Html.ActionLink("Details", "Details", new { id = item.ItemTypeID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ItemTypeID })
</td>
</tr>
}
</table>
这是我的_Layout页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("styles", required: false)
@RenderSection("scripts", required: false)
</head>
<body>
<div 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="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
<li>@Html.ActionLink("WeaponTypes", "Index", "WeaponTypes")</li>
</ul>
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© @DateTime.Now.Year - My ASP.NET Application</p>
</footer>
</div>
</body>
</html>
我想这与问题无关,只是为了“产销监管链”-BundleConfig.cs:
using System.Web;
using System.Web.Optimization;
namespace WebApplication1
{
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"));
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/site.css"));
}
}
}
因此,问题是:我想念什么?我浏览了几本手册(绝对是this one,也就是this one),这些手册只是根据示例执行的(有微小的偏差),但是仍然没有任何反应,图片是 quite the same。 PS:不用担心,数据库的内容是俄语的,没关系,请相信我,以防您不明白所写的内容:)。
EDIT
为了克服XY problem:我正在寻找在数据表中呈现数据的方式,并且: -打印表格; -创建PDF文件; -将其放入Excel(首选.xlsx)。
我看到的最好的解决方案是DataTables,遗憾的是,它仍然无法在我的代码中工作。
答案 0 :(得分:0)
要使数据表正常工作,您必须做两件事。
添加CSS
@section scripts {
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
@*!!!I included a css for datatables*@
<link rel="stylesheet" href="//cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
<script>
$(document).ready(function () {
$('#ItemTypeTable').DataTable(
);
});
</script>
}
添加thead标签
<table id="ItemTypeTable">
@*!!!NEED THE thead*@
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.ItemType1)
</th>
<th>
@Html.DisplayNameFor(model => model.WeaponType.WeaponTypeDescr)
</th>
<th></th>
</tr>
</thead>