2015-11-06 38 views
0

我遇到了使用数据表进行排序和搜索的问题。显示搜索框和箭头以进行排序。当尝试搜索或排序时,什么都没有发生,并且当有多个记录时,我的代码只显示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); 
 
     } 
 
    } 
 
}

+0

你已经得到了foreach循环里面的''标签,外移动。 – markpsmith

+0

谢谢,这工作! –

回答

0

你已经得到了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); 
 
     } 
 
    } 
 
}