使用数据表进行排序和搜索时出现问题

时间:2015-11-06 00:11:09

标签: c# jquery model-view-controller datatables

我遇到了使用数据表进行排序和搜索的问题。出现搜索框和排序箭头。当试图搜索或排序没有任何反应时,我的代码只有多个时显示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页面:

<!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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你已经在foreach循环中获得了标签,将它们移到了外面。 - markpsmith

&#13;
&#13;
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);
        }
    }
}
&#13;
&#13;
&#13;