使用数据表排序和搜索时出现问题
本文关键字:问题 搜索 数据表 排序 | 更新日期: 2023-09-27 18:20:41
我在使用数据表进行排序和搜索时遇到问题。将显示用于排序的搜索框和箭头。当尝试搜索或排序时,不会发生任何事情,并且当有多个记录时,我的代码只显示1个记录。
这是我的代码
带有表格的页面:
@model IEnumerable<WINCMUTest.Models.WINCMU_HostInfo>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table" id="thetables">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.IP_address)
</th>
<th>
@Html.DisplayNameFor(model => model.HostName)
</th>
<th>
@Html.DisplayNameFor(model => model.Zone)
</th>
<th></th>
</tr>
</thead>
@foreach (var item in Model)
{
<tbody>
<tr>
<td>
@Html.DisplayFor(modelItem => item.IP_address)
</td>
<td>
@Html.DisplayFor(modelItem => item.HostName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Zone)
</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>
我的Layout.cshtml页面(_L):
<!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>
<link href="~/Content/Site.css" rel="stylesheet" type="text/css" />
<link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="~/Content/DataTables/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="~/Scripts/modernizr-2.6.2.js"></script>
</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">
</ul>
</div>
</div>
</div>
@RenderBody()
<script src="~/Scripts/jquery-1.9.1.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
<script src="~/Scripts/DataTables/jquery.dataTables.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#thetable').dataTable();
});
</script>
</body>
</html>
我的控制器:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using WINCMUTest.Models;
namespace WINCMUTest.Controllers
{
public class WINCMU_HostInfoController : Controller
{
private WINCMUEntities db = new WINCMUEntities();
// GET: WINCMU_HostInfo
public ActionResult Index()
{
return View(db.WINCMU_HostInfo.ToList());
}
// GET: WINCMU_HostInfo/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
WINCMU_HostInfo wINCMU_HostInfo = db.WINCMU_HostInfo.Find(id);
if (wINCMU_HostInfo == null)
{
return HttpNotFound();
}
return View(wINCMU_HostInfo);
}
// GET: WINCMU_HostInfo/Create
public ActionResult Create()
{
return View();
}
// POST: WINCMU_HostInfo/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "IP_address,HostName,Zone,ID")] WINCMU_HostInfo wINCMU_HostInfo)
{
if (ModelState.IsValid)
{
db.WINCMU_HostInfo.Add(wINCMU_HostInfo);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(wINCMU_HostInfo);
}
// GET: WINCMU_HostInfo/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
WINCMU_HostInfo wINCMU_HostInfo = db.WINCMU_HostInfo.Find(id);
if (wINCMU_HostInfo == null)
{
return HttpNotFound();
}
return View(wINCMU_HostInfo);
}
// POST: WINCMU_HostInfo/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "IP_address,HostName,Zone,ID")] WINCMU_HostInfo wINCMU_HostInfo)
{
if (ModelState.IsValid)
{
db.Entry(wINCMU_HostInfo).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(wINCMU_HostInfo);
}
// GET: WINCMU_HostInfo/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
WINCMU_HostInfo wINCMU_HostInfo = db.WINCMU_HostInfo.Find(id);
if (wINCMU_HostInfo == null)
{
return HttpNotFound();
}
return View(wINCMU_HostInfo);
}
// POST: WINCMU_HostInfo/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
WINCMU_HostInfo wINCMU_HostInfo = db.WINCMU_HostInfo.Find(id);
db.WINCMU_HostInfo.Remove(wINCMU_HostInfo);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
您已经将标记放入foreach循环中,将它们移到外部。–markpsmith
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using WINCMUTest.Models;
namespace WINCMUTest.Controllers
{
public class WINCMU_HostInfoController : Controller
{
private WINCMUEntities db = new WINCMUEntities();
// GET: WINCMU_HostInfo
public ActionResult Index()
{
return View(db.WINCMU_HostInfo.ToList());
}
// GET: WINCMU_HostInfo/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
WINCMU_HostInfo wINCMU_HostInfo = db.WINCMU_HostInfo.Find(id);
if (wINCMU_HostInfo == null)
{
return HttpNotFound();
}
return View(wINCMU_HostInfo);
}
// GET: WINCMU_HostInfo/Create
public ActionResult Create()
{
return View();
}
// POST: WINCMU_HostInfo/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "IP_address,HostName,Zone,ID")] WINCMU_HostInfo wINCMU_HostInfo)
{
if (ModelState.IsValid)
{
db.WINCMU_HostInfo.Add(wINCMU_HostInfo);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(wINCMU_HostInfo);
}
// GET: WINCMU_HostInfo/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
WINCMU_HostInfo wINCMU_HostInfo = db.WINCMU_HostInfo.Find(id);
if (wINCMU_HostInfo == null)
{
return HttpNotFound();
}
return View(wINCMU_HostInfo);
}
// POST: WINCMU_HostInfo/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "IP_address,HostName,Zone,ID")] WINCMU_HostInfo wINCMU_HostInfo)
{
if (ModelState.IsValid)
{
db.Entry(wINCMU_HostInfo).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(wINCMU_HostInfo);
}
// GET: WINCMU_HostInfo/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
WINCMU_HostInfo wINCMU_HostInfo = db.WINCMU_HostInfo.Find(id);
if (wINCMU_HostInfo == null)
{
return HttpNotFound();
}
return View(wINCMU_HostInfo);
}
// POST: WINCMU_HostInfo/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
WINCMU_HostInfo wINCMU_HostInfo = db.WINCMU_HostInfo.Find(id);
db.WINCMU_HostInfo.Remove(wINCMU_HostInfo);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}