0

我正在构建一个将显示服务器信息的 ASP.Net MVC 网页。它还允许我们编辑每个服务器的所有者,而无需使用 Management Studio。

我工作正常,但我想添加一个过滤功能,以便查看我选择的服务器名称(在标题上实现过滤功能后如何过滤 Excel 电子表格)。

我一直在使用以下教程:

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/advanced-entity-framework-scenarios-for-an-mvc-web-application

一些附加信息:我在服务器上连接了一个数据库,我显示的信息来自 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);
    }
 }
}
4

1 回答 1

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 样式的过滤,但会得到您想要的响应。

于 2012-08-22T20:01:00.283 回答