我的过滤选项不起作用,但在第一页中

时间:2017-04-06 14:57:50

标签: asp.net-mvc actionlink pagedlist

我使用PagedList.Mvc进行分页时,我编写了一些ActionLink来过滤基于流派的电影商店的数据。

问题是ActionLinks正常工作,但只过滤第一页上存在的电影。我该怎么办?

查看:

@using PagedList.Mvc
@using Shop.CustomHtmlHelpers
@using Shop.Domain.Entities
@model PagedList.IPagedList<Shop.Domain.Entities.Movie>

@{
ViewBag.Title = "MovieDisplay";
}

<h2>MovieDisplay</h2>

@using (@Html.BeginForm("MovieDisplay", "Movies", FormMethod.Get))
{
<div class="container">

    <div class="row">
        <div class="col-md-10">

            @foreach (Product p in Model)
            {
                var item = (Movie)p;

                <div class="col-lg-3 col-md-3 col-sm-4 col-xs-6">
                    <div class="tile">
                        <br />
                        <b>@item.Title</b>
                        <br />
                        @item.Year
                        <br/>
                        @item.Genre
                        <br/>
                        @item.Director.Name
                        @Html.Image(@item.Image,"200")
                        <br />
                        $ @item.Price
                        <br/>
                        <br />
                        <button onclick="location.href = '@Url.Action("AddtoCart", "ShoppingCart", new {id = p.Id})'" type="button" class="btn btn-success btn-su btn-sm ">
                            <span> <i class="fa fa-shopping-basket" aria-hidden="true"></i></span>  AddtoCart
                        </button>
                        <br />
                        <br /><br />
                    </div>
                </div>
            }
        </div>
    </div>
</div>
}
@Html.ActionLink("Horror", "MovieDisplay", new { Genre = "Horror" } )
@Html.ActionLink("Action", "MovieDisplay", new { Genre = "Action" })
@Html.ActionLink("Drama", "MovieDisplay", new { Genre = "Drama" })
@Html.ActionLink("Animation", "MovieDisplay", new { Genre = "Animation" })
@Html.ActionLink("Comedy", "MovieDisplay", new { Genre = "Comedy" })
@Html.ActionLink("Crime", "MovieDisplay", new { Genre = "Crime" })
@Html.ActionLink("Sci-Fi", "MovieDisplay", new { Genre = "Sci-Fi" })
@Html.ActionLink("Fantasy", "MovieDisplay", new { Genre = "Fantasy" })
@Html.ActionLink("Historical", "MovieDisplay", new { Genre = "Historical" })
@Html.ActionLink("Musical", "MovieDisplay", new { Genre = "Musical" })

@Html.PagedListPager(Model, page => Url.Action("MovieDisplay", new { page, searchTerm = Request.QueryString["searchTerm"], Genre = "Genre" }))

相关行动结果:

public ActionResult MovieDisplay(string searchTerm, int? page , string genre)
{
    MediaContext mc = new MediaContext();

    var movies = mc.Movies.ToList().ToPagedList(page ?? 1, 6);

    if(genre !=null)
        movies = movies.Where(m => m.Genre == genre).ToList().ToPagedList(page ?? 1, 6);

    if (string.IsNullOrEmpty(searchTerm))
    { }

    else
        movies = mc.Movies.Where(x => x.Title.Contains(searchTerm)).ToList().ToPagedList(page ?? 1, 6);

    return View(movies);
}

1 个答案:

答案 0 :(得分:0)

问题只不过是我在ActionResult中犯下的一个愚蠢的错误:

movies = movies.Where(m => m.Genre == genre).ToList().ToPagedList(page ?? 1, 6);

这里我实际上过滤了现有的电影!我应该将整个电影添加到列表中,就像我为searchTerm所做的那样(这就是为什么搜索适用于所有页面的原因)

 movies = mc.Movies.Where(m => m.Genre == genre).ToList().ToPagedList(page ?? 1, 6);