我一直在關注從asp.net的優秀教程有關創建分頁和過濾:http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-applicationEF + MVC3:如何在頁面模型爲IPagedList時創建DropDownList過濾器?
我現在想用一個下拉列表,將過濾我的項目添加其他過濾器。我發現另一個關於這個教程,並遵循它:http://chikkanti.wordpress.com/2012/04/30/dropdownlist-based-filtering-in-mvc-3-using-entityframeworkedm-modalfirst-method/
但是,當我運行它時,我得到了關於下拉列表不是IEnumerable類型的錯誤。這是有意義的,因爲頁面模型是IPagedList類型的。 如何添加一個下拉列表過濾器到頁面模型IPagedList的頁面?
控制器:
public ActionResult SearchNames(string ddlcontent, int? page)
{
int pageSize = 10;
int pageNumber = (page ?? 1);
var list = new List<string>();
var nameqry = from n in db.tblProjects
select n.ProjectName;
list.AddRange(nameqry.Distinct());
ViewBag.ddlcontent = new SelectList(list);
//var names = from m in db.tblProjects
// select m;
var tbldefectdetails = db.tblDefectDetails.Include(t => t.tblBugLocation).Include(t => t.tblBugType).Include(t => t.tblBusinessUnit).Include(t => t.tblDefectDiscoveryMethod).Include(t => t.tblProject).Include(t => t.tblLanguage);
if (!string.IsNullOrEmpty(ddlcontent))
{
tbldefectdetails = tbldefectdetails.Where(s => s.tblProject.ProjectName.ToUpper().Contains(ddlcontent.ToUpper()));
}
return View(tbldefectdetails.ToPagedList(pageNumber, pageSize));
}
DROPDOWNLIST在查看:
@model PagedList.IPagedList<EDTToolMVC.Models.tblDefectDetail>
@{
ViewBag.Title = "Early Defect Tracking Tool - Home";
}
<h2>SearchNames</h2>
@using (@Html.BeginForm(「SearchNames」, 「Names」, FormMethod.Get))
{
@Html.DropDownList(「ddlcontent」, 「All」)<input type=」submit」 value=」Filter」 />;
}
IPagedList:
namespace PagedList
{
// Summary:
// Represents a subset of a collection of objects that can be individually accessed
// by index and containing metadata about the superset collection of objects
// this subset was created from.
//
// Type parameters:
// T:
// The type of object the collection should contain.
//
// Remarks:
// Represents a subset of a collection of objects that can be individually accessed
// by index and containing metadata about the superset collection of objects
// this subset was created from.
public interface IPagedList<out T> : IPagedList, IEnumerable<T>, IEnumerable
{
// Summary:
// Gets the number of elements contained on this page.
int Count { get; }
// Summary:
// Gets the element at the specified index.
//
// Parameters:
// index:
// The zero-based index of the element to get.
T this[int index] { get; }
// Summary:
// Gets a non-enumerable copy of this paged list.
//
// Returns:
// A non-enumerable copy of this paged list.
IPagedList GetMetaData();
}
}