我正在构建一个显示服务器信息的ASP.Net MVC网页。它还允许我们编辑每个服务器的所有者,而无需使用Management Studio。
我工作正常,但我想添加一个过滤功能,以便查看我选择的ServerNames(如何在标题上实现过滤功能后过滤excel电子表格)。
我一直在使用以下教程:
一些补充信息:我连接了服务器上的数据库,我显示的信息来自3个不同的表。
非常感谢任何帮助!
这是我的索引视图代码:
@model IEnumerable<SQL_Server_Inventory_UI.ServerList>
@{
ViewBag.Title = "Index";
}
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
ServerName
</th>
<th>
InstanceName
</th>
<th>
PortNumber
</th>
<th>
IsMonitored
</th>
<th>
Environment
</th>
<th>
SQLVersion
</th>
<th>
SQLServicePack
</th>
<th>
SQLProductVersion
</th>
<th>
RetireDate
</th>
<th>
IPAddress
</th>
<th>
IsVMW
</th>
<th>
Platform
</th>
<th>
IsLiteSpeedUsed
</th>
<th>
Domain
</th>
<th>
CreateDate
</th>
<th>
LastUpdateDate
</th>
<th>
CName
</th>
<th>
IsStandby
</th>
<th>
Server_Group
</th>
<th>
TeamMember
</th>
<th>
IsMissionCritical
</th>
<th>
IsCluster
</th>
<th>
DBANotes
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ServerName)
</td>
<td>
@Html.DisplayFor(modelItem => item.InstanceName)
</td>
<td>
@Html.DisplayFor(modelItem => item.PortNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsMonitored)
</td>
<td>
@Html.DisplayFor(modelItem => item.Environment)
</td>
<td>
@Html.DisplayFor(modelItem => item.SQLVersion)
</td>
<td>
@Html.DisplayFor(modelItem => item.SQLServicePack)
</td>
<td>
@Html.DisplayFor(modelItem => item.SQLProductVersion)
</td>
<td>
@Html.DisplayFor(modelItem => item.RetireDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.IPAddress)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsVMW)
</td>
<td>
@Html.DisplayFor(modelItem => item.Platform)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsLiteSpeedUsed)
</td>
<td>
@Html.DisplayFor(modelItem => item.Domain)
</td>
<td>
@Html.DisplayFor(modelItem => item.CreateDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastUpdateDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.CName)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsStandby)
</td>
<td>
@Html.DisplayFor(modelItem => item.Server_Group.GroupName)
</td>
<td>
@Html.DisplayFor(modelItem => item.TeamMember.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsMissionCritical)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsCluster)
</td>
<td>
@Html.DisplayFor(modelItem => item.DBANotes)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ServerId }) |
@Html.ActionLink("Details", "Details", new { id=item.ServerId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ServerId })
</td>
</tr>
}
</table>
这是我的控制器代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SQL_Server_Inventory_UI;
namespace SQL_Server_Inventory_UI.Controllers
{
public class ServerListController : Controller
{
private ServerListConnectionEntities db = new ServerListConnectionEntities();
//
// GET: /ServerList/
public ViewResult Index()
{
var serverlists = db.ServerLists.Include("Server_Group").Include("TeamMember");
return View(serverlists.ToList());
}
//
// GET: /ServerList/Details/5
public ViewResult Details(int id)
{
ServerList serverlist = db.ServerLists.Single(s => s.ServerId == id);
return View(serverlist);
}
//
// GET: /ServerList/Create
public ActionResult Create()
{
ViewBag.GroupID = new SelectList(db.Server_Group, "GroupID", "GroupName");
ViewBag.OwnerID = new SelectList(db.TeamMembers, "MemberID", "Name");
return View();
}
//
// POST: /ServerList/Create
[HttpPost]
public ActionResult Create(ServerList serverlist)
{
if (ModelState.IsValid)
{
db.ServerLists.AddObject(serverlist);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.GroupID = new SelectList(db.Server_Group, "GroupID", "GroupName", serverlist.GroupID);
ViewBag.OwnerID = new SelectList(db.TeamMembers, "MemberID", "Name", serverlist.OwnerID);
return View(serverlist);
}
//
// GET: /ServerList/Edit/5
public ActionResult Edit(int id)
{
ServerList serverlist = db.ServerLists.Single(s => s.ServerId == id);
ViewBag.GroupID = new SelectList(db.Server_Group, "GroupID", "GroupName", serverlist.GroupID);
ViewBag.OwnerID = new SelectList(db.TeamMembers, "MemberID", "Name", serverlist.OwnerID);
return View(serverlist);
}
//
// POST: /ServerList/Edit/5
[HttpPost]
public ActionResult Edit(ServerList serverlist)
{
if (ModelState.IsValid)
{
db.ServerLists.Attach(serverlist);
db.ObjectStateManager.ChangeObjectState(serverlist, EntityState.Modified);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.GroupID = new SelectList(db.Server_Group, "GroupID", "GroupName", serverlist.GroupID);
ViewBag.OwnerID = new SelectList(db.TeamMembers, "MemberID", "Name", serverlist.OwnerID);
return View(serverlist);
}
//
// GET: /ServerList/Delete/5
public ActionResult Delete(int id)
{
ServerList serverlist = db.ServerLists.Single(s => s.ServerId == id);
return View(serverlist);
}
//
// POST: /ServerList/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
ServerList serverlist = db.ServerLists.Single(s => s.ServerId == id);
db.ServerLists.DeleteObject(serverlist);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
答案 0 :(得分:0)
将您的Index
控制器更改为以下内容:
public ViewResult Index(string filter)
{
var serverlists = db.ServerLists.Include("Server_Group").Include("TeamMember");
if (!string.IsNullorEmpty(filter))
serverlists = serverlists.Where(sl => sl.Name.StartsWith(filter);
return View(serverlists.ToList());
}
然后,当您需要过滤列表时,将视图更改为使用过滤器{url}?filter=stringtosearchfor
进行回发。这不会给你AJAX风格的过滤,但会得到你想要的回应。