[.NET MVC4 入门系列05]添加自定义查询页Search

来源:互联网 发布:淘宝哪家毛边纸好 编辑:程序博客网 时间:2024/06/08 04:12

一、简介&目标:

这一节中,学习添加Search方法和Search视图.

方法名:SearchIndex

视图路径:/Movies/SearchIndex

功能:用户可以通过关键字查找自己感兴趣的电影

提供两种查询条件:电影名关键字、电影种类,如图

http://localhost:54782/Movies/SearchIndex?movieGenre=%E9%AD%94%E5%B9%BB&SearchString=2

这个是查询时,生成的的URL,GET方式,包含QueryString作为查询条件:“?movieGenre=%E9%AD%94%E5%B9%BB&SearchString=2”

 

二、添加控制器Action方法:

查询Search不需要对数据进行更改,所以Action方法只需要使用Get方式即可,所以,在Movie的Controller代码中,添加下面方法:

复制代码
 1         // 2         //GET: /Movies/SearchIndex 3         public ActionResult SearchIndex(string movieGenre,string searchString) 4         { 5             //准备种类列表数据源GereLst 6             var GenreLst = new List<string>(); 7             var GenreQry = from d in db.Movies 8                            orderby d.Genre 9                            select d.Genre;10             GenreLst.AddRange(GenreQry.Distinct());11 12             ViewBag.movieGenre = new SelectList(GenreLst);13 14             var movies = from m in db.Movies15                          select m;16             if (!String.IsNullOrEmpty(searchString))17             {18                 movies = movies.Where(s => s.Title.Contains(searchString));19             }20 21             if (string.IsNullOrEmpty(movieGenre))22             {23                 return View(movies);24             }25             else26             { 27                 return View(movies.Where(x=>x.Genre==movieGenre));28             }29 30             return View(movies);31         }
复制代码

【代码解析】

  1.方法前面没有[HttpPost]所以,是Get方式向服务器传值,值应该包含在URL的QueryString中,对应方法的两个参数:

    string movieGenre(电影种类),string searchString(电影名关键字);

  2. GenreLst,用来填充查询条件中的电影种类下拉列表,查处后,通过ViewBag.movieGenre传递给View;

  3. movies最多可能会有三次赋值:

  •  第一次赋值获取到所有电影,这个赋值必定执行;
  •  第二次赋值条件是电影名关键字不为空,赋值后获取到电影命中包含指定关键字电影信息;
  •  第三次赋值条件是指定电影种类,赋值后获取到指定电影类型的电影信息。

      4. 最终,返回到符合查询条件的电影查询视图。

三、添加View

  新建视图到/movies/views/中,配置如图:

  

       更改View中代码:

复制代码
 1 @model IEnumerable<MvcApplication1.Models.Movie> 2  3 @{ 4     ViewBag.Title = "SearchIndex"; 5 } 6  7 <h2>SearchIndex</h2> 8  9 <p>10     @Html.ActionLink("Create New", "Create")11     @using (Html.BeginForm("SearchIndex","Movies",FormMethod.Get))12     { 13         <p>14             Genre:@Html.DropDownList("movieGenre", "All")15             Title:@Html.TextBox("SearchString")<br />16             <input type="submit" value="Filter" />17         </p>18     }19 </p>20 <table>21     <tr>22         <th>23             @Html.DisplayNameFor(model => model.Title)24         </th>25         <th>26             @Html.DisplayNameFor(model => model.ReleaseDate)27         </th>28         <th>29             @Html.DisplayNameFor(model => model.Genre)30         </th>31         <th>32             @Html.DisplayNameFor(model => model.Price)33         </th>34         <th></th>35     </tr>36 37 @foreach (var item in Model) {38     <tr>39         <td>40             @Html.DisplayFor(modelItem => item.Title)41         </td>42         <td>43             @Html.DisplayFor(modelItem => item.ReleaseDate)44         </td>45         <td>46             @Html.DisplayFor(modelItem => item.Genre)47         </td>48         <td>49             @Html.DisplayFor(modelItem => item.Price)50         </td>51         <td>52             @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |53             @Html.ActionLink("Details", "Details", new { id=item.ID }) |54             @Html.ActionLink("Delete", "Delete", new { id=item.ID })55         </td>56     </tr>57 }58 59 </table>
复制代码

  【代码解析】

  核心代码:

复制代码
1  @using (Html.BeginForm("SearchIndex","Movies",FormMethod.Get))2     { 3         <p>4             Genre:@Html.DropDownList("movieGenre", "All")5             Title:@Html.TextBox("SearchString")<br />6             <input type="submit" value="Filter" />7         </p>8     }
复制代码

  1.Html.BeginForm("SearchIndex","Movies",FormMethod.Get)

  HtmlHelper的BeginForm方法,用来按指定的方式向服务器提交信息,并开始Form表单。

  包含三个参数:

    第一个:“SearchIndex”,控制器中指定的Action方法;

    第二个:“Movies”,控制器名称;

    第三个:FormMethod.Get,向服务器提交信息的方式,Get方式

  2.Form表单中包含的就是那个<p>,其中包含三部分:

   1)电影类型下拉列表:Genre:@Html.DropDownList("movieGenre""All")

     通过movieGenre(Controller的Action方法中通过ViewBag赋值)填充,默认项显示:All

   2)文本框,用来获取电影名称关键字:Title:@Html.TextBox("SearchString")

   3)提交按钮

  3.下面的table是在创建视图时,选定的List模板自动生成的,其中的绑定项是选择强类型Movie类自动指定的。

 

四:扩展阅读:

    Lambda表达式:s=>s.Title,本文中,使用在Linq查询的方法中,充当参数:

   movies = movies.Where(=> s.Title.Contains(searchString));

  此查询对应的Sql语句为:

select * from movie where title like '%'+@searchString+'%'
0 0
原创粉丝点击