0

ページネーションとフィルタリングの作成に関するasp.netの優れたチュートリアルに従っています:http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and- asp-net-mvc-application 内のエンティティ フレームワークを使用したページング

プロジェクトをフィルタリングするドロップダウンリストを使用して、別のフィルターを追加したいと考えています。これに関する別のチュートリアルを見つけて、それに従いました: 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 に答える