ASP.Net MVC Filtering

本文关键字:Filtering MVC Net ASP | 更新日期: 2023-09-27 17:57:23

THEAPP:ASP.NET MVC克鲁德应用Visual Studio 2013

做:通过将列(扇区)值呈现到下拉列表中来创建过滤器选项。因此,用户可以从下拉列表中选择一个扇区,然后单击提交以过滤表中的记录

要求:根据列(扇区)值筛选表上的记录。

在记录表中,假设列 [扇区] 有 =>IT, 管理, 营销, ABC, XYZ,

需要像导航栏一样获取所有扇区值。单击此列表的部门(营销)后,表格应列出仅具有该部门(营销)的记录

注意:用户能够插入新记录,因此能够创建新的扇区名称,因此呈现到导航栏的链接应该是动态的

由于我是该语言和MVC的新手,我不知道如何不使用下拉列表使用列表视图

控制器代码

PipelineController.cshtml

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 PipelineApp.Models;
namespace PipelineApp.Controllers
{
    public class PipelineController : Controller
    {
        private PipelineEntities db = new PipelineEntities();
        // GET: /Pipeline/
        //public ActionResult Index()
        //{
        //    return View(db.Pipelinedatas.ToList());
        //}
        //CUSTOM GET: /Pipelinedata/Sector filtering
        //public ActionResult Index(string sector)
        //{
        //    ViewBag.Sector = (from r in db.Pipelinedatas
        //                      select r.Sector).Distinct();
        //    var model = from r in db.Pipelinedatas
        //                where r.Sector == sector || sector == null || sector == ""
        //                select r;
        //    return View(model);
        //}


        //CUSTOM GET: /Pipelinedata/ Sector Filtering/ Sort Order
        public ActionResult Index(string sector, string sortOrder, int? page)
        {
            //Filter Secors ------------------------------
            ViewBag.Sector = (from r in db.Pipelinedatas
                              select r.Sector).Distinct();
            //---------------------------------------------
            var model = from r in db.Pipelinedatas
                        where r.Sector == sector || sector == null || sector == ""
                        select r;

            //Sort Order ----------------------------------
            ViewBag.CurrentSort = sortOrder; //Paging
            ViewBag.EmployerSortParm = String.IsNullOrEmpty(sortOrder) ? "emp_name" : "";
            ViewBag.ITOSortParm = String.IsNullOrEmpty(sortOrder) ? "ITO" : "";
            ViewBag.JanSortParm = String.IsNullOrEmpty(sortOrder) ? "January" : "";
            ViewBag.FebSortParm = String.IsNullOrEmpty(sortOrder) ? "February" : "";
            ViewBag.MarSortParm = String.IsNullOrEmpty(sortOrder) ? "March" : "";
            ViewBag.AprSortParm = String.IsNullOrEmpty(sortOrder) ? "April" : "";
            ViewBag.MaySortParm = String.IsNullOrEmpty(sortOrder) ? "May" : "";
            ViewBag.JunSortParm = String.IsNullOrEmpty(sortOrder) ? "June" : "";
            ViewBag.JulSortParm = String.IsNullOrEmpty(sortOrder) ? "July" : "";
            ViewBag.AugSortParm = String.IsNullOrEmpty(sortOrder) ? "August" : "";
            ViewBag.SepSortParm = String.IsNullOrEmpty(sortOrder) ? "September" : "";
            ViewBag.OctSortParm = String.IsNullOrEmpty(sortOrder) ? "October" : "";
            ViewBag.NovSortParm = String.IsNullOrEmpty(sortOrder) ? "November" : "";
            ViewBag.DecSortParm = String.IsNullOrEmpty(sortOrder) ? "December" : "";
            ViewBag.SectorSortParm = sortOrder == "sec" ? "ITO" : "sec";
            switch (sortOrder)
            {
                case "emp_name":
                    model = model.OrderByDescending(s => s.Employer);
                    break;
                case "sec":
                    model = model.OrderBy(s => s.Sector);
                    break;
                case "ITO":
                    model = model.OrderByDescending(s => s.ITONumber);
                    break;
                case "January":
                    model = model.OrderByDescending(s => s.Jan);
                    break;
                case "February":
                    model = model.OrderByDescending(s => s.Feb);
                    break;
                case "March":
                    model = model.OrderByDescending(s => s.Mar);
                    break;
                case "April":
                    model = model.OrderByDescending(s => s.Apr);
                    break;
                case "May":
                    model = model.OrderByDescending(s => s.May);
                    break;
                case "June":
                    model = model.OrderByDescending(s => s.Jun);
                    break;
                case "July":
                    model = model.OrderByDescending(s => s.Jul);
                    break;
                case "August":
                    model = model.OrderByDescending(s => s.Aug);
                    break;
                case "September":
                    model = model.OrderByDescending(s => s.Sep);
                    break;
                case "October":
                    model = model.OrderByDescending(s => s.Oct);
                    break;
                case "November":
                    model = model.OrderByDescending(s => s.Nov);
                    break;
                case "December":
                    model = model.OrderByDescending(s => s.Dec);
                    break;
                default:
                    model = model.OrderBy(s => s.Id);
                    break;
            }
            //---------------------------------------------
            //Paging --------------------------------------
            //int pageSize = 3;
            //int pageNumber = (page ?? 1);
            //return View(model.ToPagedList(pageNumber, pageSize));
            //---------------------------------------------
            return View(model);
        }


        // GET: /Pipeline/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Pipelinedata pipelinedata = db.Pipelinedatas.Find(id);
            if (pipelinedata == null)
            {
                return HttpNotFound();
            }
            return View(pipelinedata);
        }
        // GET: /Pipeline/Create
        public ActionResult Create()
        {
            return View();
        }
        // POST: /Pipeline/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="Id,Employer,ITONumber,Description,TECNumber,TECVersion,Level,Credits,Duration,Sector,Status,Approval,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec")] Pipelinedata pipelinedata)
        {
            if (ModelState.IsValid)
            {
                db.Pipelinedatas.Add(pipelinedata);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(pipelinedata);
        }
        // GET: /Pipeline/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Pipelinedata pipelinedata = db.Pipelinedatas.Find(id);
            if (pipelinedata == null)
            {
                return HttpNotFound();
            }
            return View(pipelinedata);
        }
        // POST: /Pipeline/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="Id,Employer,ITONumber,Description,TECNumber,TECVersion,Level,Credits,Duration,Sector,Status,Approval,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec")] Pipelinedata pipelinedata)
        {
            if (ModelState.IsValid)
            {
                db.Entry(pipelinedata).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(pipelinedata);
        }
        // GET: /Pipeline/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Pipelinedata pipelinedata = db.Pipelinedatas.Find(id);
            if (pipelinedata == null)
            {
                return HttpNotFound();
            }
            return View(pipelinedata);
        }
        // POST: /Pipeline/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Pipelinedata pipelinedata = db.Pipelinedatas.Find(id);
            db.Pipelinedatas.Remove(pipelinedata);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

查看代码

Index.cshtml

@model IEnumerable<PipelineApp.Models.Pipelinedata>
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
    <text> Sectors </text>
    @Html.DropDownList("sector", new SelectList(ViewBag.Sector))
    <input class="btn" type="submit" value="Filter" />
}

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.ActionLink("Employer", "Index", new { sortOrder = ViewBag.EmployerSortParm })
            @*@Html.DisplayNameFor(model => model.Employer)*@
        </th>
        <th>
            @Html.ActionLink("ITONumber", "Index", new { sortOrder = ViewBag.ITOSortParm })
            @*@Html.DisplayNameFor(model => model.ITONumber)*@
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Description)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TECNumber)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TECVersion)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Level)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Credits)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Duration)
        </th>
        <th>
            @Html.ActionLink("Sector", "Index", new { sortOrder = ViewBag.SectorSortParm })
            @*@Html.DisplayNameFor(model => model.Sector)*@
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Status)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Approval)
        </th>
        <th>
            @Html.ActionLink("Jan", "Index", new { sortOrder = ViewBag.JanSortParm })
            @*@Html.DisplayNameFor(model => model.Jan)*@
        </th>
        <th>
            @Html.ActionLink("Feb", "Index", new { sortOrder = ViewBag.FebSortParm })
            @*@Html.DisplayNameFor(model => model.Feb)*@
        </th>
        <th>
            @Html.ActionLink("Mar", "Index", new { sortOrder = ViewBag.MarSortParm })
            @*@Html.DisplayNameFor(model => model.Mar)*@
        </th>
        <th>
            @Html.ActionLink("Apr", "Index", new { sortOrder = ViewBag.AprSortParm })
            @*@Html.DisplayNameFor(model => model.Apr)*@
        </th>
        <th>
            @Html.ActionLink("May", "Index", new { sortOrder = ViewBag.MaySortParm })
            @*@Html.DisplayNameFor(model => model.May)*@
        </th>
        <th>
            @Html.ActionLink("Jun", "Index", new { sortOrder = ViewBag.JunSortParm })
            @*@Html.DisplayNameFor(model => model.Jun)*@
        </th>
        <th>
            @Html.ActionLink("Jul", "Index", new { sortOrder = ViewBag.JulSortParm })
            @*@Html.DisplayNameFor(model => model.Jul)*@
        </th>
        <th>
            @Html.ActionLink("Aug", "Index", new { sortOrder = ViewBag.AugSortParm })
            @*@Html.DisplayNameFor(model => model.Aug)*@
        </th>
        <th>
            @Html.ActionLink("Sep", "Index", new { sortOrder = ViewBag.SepSortParm })
            @*@Html.DisplayNameFor(model => model.Sep)*@
        </th>
        <th>
            @Html.ActionLink("Oct", "Index", new { sortOrder = ViewBag.OctSortParm })
            @*@Html.DisplayNameFor(model => model.Oct)*@
        </th>
        <th>
            @Html.ActionLink("Nov", "Index", new { sortOrder = ViewBag.NovSortParm })
            @*@Html.DisplayNameFor(model => model.Nov)*@
        </th>
        <th>
            @Html.ActionLink("Dec", "Index", new { sortOrder = ViewBag.DecSortParm })
            @*@Html.DisplayNameFor(model => model.Dec)*@
        </th>
        <th></th>
    </tr>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Employer)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ITONumber)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Description)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TECNumber)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TECVersion)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Level)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Credits)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Duration)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Sector)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Status)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Approval)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Jan)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Feb)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Mar)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Apr)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.May)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Jun)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Jul)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Aug)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Sep)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Oct)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Nov)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Dec)
        </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>
}
</table>

引用:http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/filter-records-in-mvc/http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

ASP.Net MVC Filtering

只需使用 ajax 调用操作方法"索引",在您的情况下,单击导航栏中的选项卡即可。你的ajax代码看起来像这样。

$.ajax({
    type: "GET",
    url: "/SomeController/Index",
    data: { 'sector': selectedSector },
    success: function (data) {
       // Here you can assign the partial view returned to an encompassing div
       $('#sectorDiv').html(data); 
    },
    error: function (xhr, status, message) {
    }
});
    @using (Ajax.BeginForm("Index",new AjaxOptions { UpdateTargetId = "result" }))
    {
        @Html.DropDownList("sector", new SelectList(ViewBag.Sector), new { @class = "chzn-select select-block-level" })
        <input class="btn" type="submit" value="Filter" />
    }
<div id="result">
@Html.Partial("gridPartial")
</div>