MVC05
2021-04-23 04:26
                         标签:distinct   移除   get   info   sele   NPU   关键字查询   key   opd    1. 添加搜索功能 如何实现url添加查询字符串实现查询指定项目的功能? 来到MovisController,修改Index方法如下 修改完毕打开页面,在Movies页面下使用查询字符串进行关键字查询即可。 https://localhost:44366/Movies?queryString=keywords 即可筛选出标题包含关键字的项目。   2.通过页面ui进行搜索 直接到View目录下的Index进行修改即可: 
    @Html.ActionLink("Create New", "Create")
    添加搜索功能
    @using (Html.BeginForm())
    {
         
            Title:@Html.TextBox("QueryString")  指定参数与请求方法: Html.BeginForm("ActionName","ControllerName",FormMethod.method) 
            Title:@Html.TextBox("QueryString")  通过post方式进行查找   3.根据其他字段进行搜索 上述的搜索功能都是依据影片的名称进行查找,现根据其他字段进行查找 TextBox,DropdownList的第二个参数都是默认值 修改Controller Index方法如下:   修改View如下: 
    @Html.ActionLink("Create New", "Create")
    添加搜索功能
    @using (Html.BeginForm("Index","Movies",FormMethod.Get))
    {
         
            Genre:@Html.DropDownList("movieGenre", "All")   MVC05 标签:distinct   移除   get   info   sele   NPU   关键字查询   key   opd    原文地址:https://www.cnblogs.com/Tanqurey/p/12240035.html        public ActionResult Index(string queryString)
        {
            var movies = from m in db.Movies select m;
            if(!String.IsNullOrEmpty(queryString))
            {
                movies = movies.Where(s => s.Title.Contains(queryString));
            }
            return View(movies);
        }
"submit" value="Filter" />
          @using (Html.BeginForm("Index","Movies",FormMethod.Get))
    {
        
"submit" value="Filter" />
        [HttpPost]
        public string Index(FormCollection fc,string queryString)
        {
            return "
from post method+
"+ queryString + "";
        }  public ActionResult Index(string movieGenre,string queryString)
        {
            var GenreList = new Liststring>();
            var GenreQry = from d in db.Movies
                           orderby d.Genre
                           select d.Genre;
            // 添加至list并移除重复项
            GenreList.AddRange(GenreQry.Distinct());
            // 传递给View页面
            ViewBag.movieGenre = new SelectList(GenreList);
            var movies = from m in db.Movies select m;
            // 先根据题目查找再根据类别查找
            if(!String.IsNullOrEmpty(queryString))
            {
                movies = movies.Where(s => s.Title.Contains(queryString));
            }
            if (!String.IsNullOrEmpty(movieGenre))
            {
                movies = movies.Where(x => x.Genre==movieGenre);
            }
            return View(movies);
        }
            Title:@Html.TextBox("QueryString") 
"submit" value="Filter" />