2016-06-22
這篇是參考下面這篇的學習,不過工具從 Visual Studio 2013 改成 Visual Studio 2015 with Update 2 繁體中文版;網頁上有提到一篇新的改用 Visual Studio 2015,但是那篇要另外安裝 ASP.NET Core,而目前最新為 ASP.NET Code 1.0.0 RC2,並非正式版,所以暫時不想安裝和參考那篇。
內容不是完全翻譯,因為練習的心得,有增加、刪減圖片和文字。
Getting Started with ASP.NET MVC 5
http://www.asp.net/mvc/overview/getting-started/introduction/getting-started
請先看這篇
[研究] ASP.NET MVC 5入門(七)瞭解 Edit Method 和 Edit View
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5-edit-method-edit-view.html
8. Adding a Search Method and Search View
http://www.asp.net/mvc/overview/getting-started/introduction/adding-search
把 Controllers\MoviesController.cs 檔案中 Index() 內容
public ActionResult Index() { return View(db.Movies.ToList()); } |
public ActionResult Index(string searchString) { var movies = from m in db.Movies select m; if (!String.IsNullOrEmpty(searchString)) { movies = movies.Where(s => s.Title.Contains(searchString)); } return View(movies); } |
(下圖) 網址 http://localhost:38857/Movies/index 畫面如下
( 有 2 筆資料請自己手動增加 )
(下圖) 網址 http://localhost:38857/Movies/index?searchString=ghost 畫面如下
因為檔案 App_Start\RouteConfig.cs 內容的路由格式為
{controller}/{action}/{id} |
public ActionResult Index(string searchString) { var movies = from m in db.Movies select m; if (!String.IsNullOrEmpty(searchString)) { movies = movies.Where(s => s.Title.Contains(searchString)); } return View(movies); } |
public ActionResult Index(string id) { string searchString = id; var movies = from m in db.Movies select m; if (!String.IsNullOrEmpty(searchString)) { movies = movies.Where(s => s.Title.Contains(searchString)); } return View(movies); } |
網址可以簡化為 http://localhost:38857/Movies/index/ghost
每次要在網址輸入要搜尋的字串太麻煩,修改程式
把 Controllers\MoviesController.cs 檔案中 Index() 內容從
public ActionResult Index(string id) { string searchString = id; var movies = from m in db.Movies select m; if (!String.IsNullOrEmpty(searchString)) { movies = movies.Where(s => s.Title.Contains(searchString)); } return View(movies); } |
public ActionResult Index(string searchString) { var movies = from m in db.Movies select m; if (!String.IsNullOrEmpty(searchString)) { movies = movies.Where(s => s.Title.Contains(searchString)); } return View(movies); } |
再修改 Views\Movies\Index.cshtml 內容
@model IEnumerable<MvcMovie.Models.Movie> @{ ViewBag.Title = "Index"; } <h2>Index</h2> <p> @Html.ActionLink("Create New", "Create") @using (Html.BeginForm()){ <p> Title: @Html.TextBox("SearchString") <br /> <input type="submit" value="Filter" /></p> } </p> |
增加搜尋 Genre 欄位
把 Controllers\MoviesController.cs 檔案中 Index() 內容從
public ActionResult Index(string searchString) { var movies = from m in db.Movies select m; if (!String.IsNullOrEmpty(searchString)) { movies = movies.Where(s => s.Title.Contains(searchString)); } return View(movies); } |
public ActionResult Index(string movieGenre, string searchString) { var GenreLst = new List<string>(); var GenreQry = from d in db.Movies orderby d.Genre select d.Genre; GenreLst.AddRange(GenreQry.Distinct()); ViewBag.movieGenre = new SelectList(GenreLst); var movies = from m in db.Movies select m; if (!String.IsNullOrEmpty(searchString)) { movies = movies.Where(s => s.Title.Contains(searchString)); } if (!string.IsNullOrEmpty(movieGenre)) { movies = movies.Where(x => x.Genre == movieGenre); } return View(movies); } |
修改 Views\Movies\Index.cshtml,增加顯示 Genre 欄位
@model IEnumerable<MvcMovie.Models.Movie> @{ ViewBag.Title = "Index"; } <h2>Index</h2> <p> @Html.ActionLink("Create New", "Create") @using (Html.BeginForm()) { <p> Genre: @Html.DropDownList("movieGenre", "All") Title: @Html.TextBox("SearchString") <br /> <input type="submit" value="Filter" /> </p> } </p> <table class="table"> |
Genre: @Html.DropDownList("movieGenre", "All") 是
Controllers\MoviesController.cs 檔案中 Index() 內容
ViewBag.movieGenre = new SelectList(GenreLst);
提供的
Genre: @Html.DropDownList("movieGenre", "All") 是
可以改為
Genre: @Html.DropDownList("movieGenre", "Comedy")
表示只找 genre 欄位值是 Comedy 的
(完)
[研究] ASP.NET MVC 5入門(一)開始
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5_21.html
[研究] ASP.NET MVC 5入門(二)增加控制器(Controller)
[研究] ASP.NET MVC 5入門(三)增加檢視(View)
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5view.html
[研究] ASP.NET MVC 5入門(六)從 Controller 存取 Model 的資料
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5-controller-model.html
[研究] ASP.NET MVC 5入門(四)增加模型(Model)
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5model.html
[研究] ASP.NET MVC 5入門(五)建立 SQL Server LocalDB 的連線字串
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5-sql-server-localdb.html
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5model.html
[研究] ASP.NET MVC 5入門(五)建立 SQL Server LocalDB 的連線字串
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5-sql-server-localdb.html
[研究] ASP.NET MVC 5入門(六)從 Controller 存取 Model 的資料
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5-controller-model.html
[研究] ASP.NET MVC 5入門(七)瞭解 Edit Method 和 Edit View
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5-edit-method-edit-view.html
[研究] ASP.NET MVC 5入門(八)增加搜尋 Method 和 View
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5-method-view.html
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5-edit-method-edit-view.html
[研究] ASP.NET MVC 5入門(八)增加搜尋 Method 和 View
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5-method-view.html
沒有留言:
張貼留言