0

我一直在关注 asp.net 关于创建分页和过滤的优秀教程:http ://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-在-asp-net-mvc-应用程序中使用实体框架进行分页

我现在想使用下拉列表添加另一个过滤器来过滤我的项目。我找到了另一个关于这个的教程并遵循它: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));
    }

视图中的下拉列表:

@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();
}
}
4

1 回答 1

0

修改视图模型,使其包含下拉列表的元素。或者从 IPagedList 继承。

class MyViewModel<T>{
  public IPagedList<T> Model {get;set;}
  public IList<something> DropdownElements {get;set;}
}

或者为了使其可重用,创建一个基本 Viewmodel 或一个基本 View 类,您的 ViewModel 或 View 从中继承包含实现此类常见可重用“服务”的函数。

于 2012-07-11T09:06:45.127 回答