一、簡介&目標:
這一節中,學習添加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.Movies 15 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 else 26 { 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 => s.Title.Contains(searchString));
此查詢對應的Sql語句為:
select * from movie where title like '%'+@searchString+'%'
初學MS 的MVC 4,參照微軟www.asp.net/mvc 中的入門項目,寫個MVC 4的入門系列,以供復習和分享。
微軟入門項目:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4
【目錄】
1.[.NET MVC4 入門系列01]Helloworld MVC 4 第一個MVC4程序
2. [.NET MVC4 入門系列02]MVC Movie 為項目添加Model
3. [.NET MVC4 入門系列03]使用Controller訪問Model中數據
4. [.NET MVC4 入門系列04]Controller和View間交互原理
5. .NET MVC4 入門系列05]添加自定義查詢頁Search
6. [.NET MVC4 入門系列06] 在Movie Model和表中添加新字段(Code First Migrations)
7. [.NET MVC4 入門系列07] 在Model模型模塊中添加驗證