【问题标题】:Filter is not working on page with asp.net过滤器在使用 asp.net 的页面上不起作用
【发布时间】:2015-01-02 18:25:15
【问题描述】:

我们正在 asp.net 中为学校制作一个项目,我们有一个包含我们添加的电影的页面,但现在我们想要一个过滤流派的页面。我们编写的代码不起作用,经过搜索我们仍然没有找到正确的答案。谁能帮我们找到它?

这是控制器代码

   public class AllMoviesController : Controller
        {
            private MovieBankEntities db = new MovieBankEntities();

            // GET: AllMovies
            public ActionResult Index(int? page, string searchString, string searchByGenre)
            {
                var movies = db.movies.Include(m => m.genres);
                IQueryable<movie> movieByGenre = db.movies;


                //Dropdownlist
                var allMovies = from m in db.movies
                                select m;

                var allGenres = from g in db.genres
                                select g;


                ViewBag.searchByGenre = new SelectList(allGenres, "genre1", "genre1");

                if (!string.IsNullOrEmpty(searchByGenre))
                {
                    allGenres = allGenres.Where(s => s.genre1 == searchByGenre);
                }

                //Searchbox


                if (!String.IsNullOrEmpty(searchString))
                {
                    allMovies = allMovies.Where(s => s.movieTitle.Contains(searchString));
                }

                return View(allMovies.ToList().ToPagedList(page ?? 1, 12));
            }

这是我们的观点:

<div class="positionSearchbox">
    <div class="positionDropList">
        @using (Html.BeginForm("Index", "AllMovies", FormMethod.Get))
        {
            <p>
               Genre : @Html.DropDownList("searchByGenre", "Select a genre")
                <input type="submit" value="Filter" class="btn btn-danger" />
        <p>Search Movie</p>
                @using (Html.BeginForm(new { @class = "form-search" }))
                {

                    @Html.TextBox("SearchString", "", new
        {
            style = "background-color: #f9f9f9; font-size: 16px; height: 35px; width: 130px; padding-left:10px; padding-right: 10px; -moz-border-radius: 2px; -webkit-border-radius: 2px; border-radius: 2px; float:left; margin-top:-35px; margin-left:100px;"

        })

                    @*<div>@Html.TextBox("SearchString") &nbsp;&nbsp; <input class="bntSearch" type="submit" value="Search" /></div>*@
                }

            </p>
        }
    </div>


</div>

【问题讨论】:

  • 定义“不工作”。它究竟是如何失败的?
  • 点击按钮时没有过滤。当我们查看 url 时它正在工作:localhost:59596/AllMovies?searchByGenre=Comedy 但是视图没有做任何事情,点击过滤按钮后显示所有电影

标签: c# asp.net filter


【解决方案1】:

电影和流派之间有什么关系?

在您的代码中,您正在过滤流派集合:

if (!string.IsNullOrEmpty(searchByGenre))
{
    allGenres = allGenres.Where(s => s.genre1 == searchByGenre);
}

但是你实际上从来没有allGenres 变量做任何事情。您只将电影返回到视图中:

return View(allMovies.ToList().ToPagedList(page ?? 1, 12));

听起来您想按所选类型过滤电影。电影是否具有导航到流派对象的.Genre 属性?像这样?:

if (!string.IsNullOrEmpty(searchByGenre))
{
    allMovies = allMovies.Where(m => m.Genre.genre1 == searchByGenre);
}

或者这些对象是否以其他方式相关?由于您没有显示对象的结构,我只能猜测。但关键是allGenres 变量在代码中似乎没有任何用途。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2013-08-12
    • 2015-08-05
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 2018-12-14
    相关资源
    最近更新 更多