<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                在本節中,您將開始修改為電影控制器所新加的操作方法和視圖。然后,您將添加一個自定義的搜索頁。 在瀏覽器地址欄里追加/Movies, 瀏覽到Movies頁面。并進入編輯(Edit)頁面。 [![clip_image001](https://box.kancloud.cn/2016-08-08_57a81c67cdf35.png "clip_image001")](http://images.cnitblog.com/blog/139239/201301/24114514-f24bbdfa4ce74b3fbccc898a4f00c292.png) **Edit(編輯)**鏈接是由*Views\Movies\Index.cshtml*視圖中的`Html.ActionLink`方法所生成的: ~~~ @Html.ActionLink("Edit", "Edit", new { id=item.ID }) ~~~ [![clip_image002](https://box.kancloud.cn/2016-08-08_57a81c67e694d.png "clip_image002")](http://images.cnitblog.com/blog/139239/201301/24114516-fcc873c8ab924b4ba74f075535bf555c.png) `Html`對象是一個Helper, 以屬性的形式, 在[System.Web.Mvc.WebViewPage](http://msdn.microsoft.com/en-us/library/gg402107(VS.98).aspx)基類上公開。[ActionLink](http://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink.aspx)是一個幫助方法,便于動態生成指向Controller中操作方法的HTML 超鏈接鏈接。`ActionLink`方法的第一個參數是想要呈現的鏈接文本 (例如,`<a>Edit Me</a>`)。第二個參數是要調用的操作方法的名稱。最后一個參數是一個[匿名對象](http://weblogs.asp.net/scottgu/archive/2007/05/15/new-orcas-language-feature-anonymous-types.aspx),用來生成路由數據 (在本例中,ID 為 4 的)。 在上圖中所生成的鏈接是*http://localhost:xxxxx/Movies/Edit/4*默認的路由 (在*App_Start\RouteConfig.cs* 中設定) 使用的 URL 匹配模式為:`{controller}/{action}/{id}`。因此,ASP.NET 將*http://localhost:xxxxx/Movies/Edit/4*轉化到`Movies` 控制器中`Edit`操作方法,參數`ID`等于 4 的請求。查看*App_Start\RouteConfig.cs*文件中的以下代碼。 ~~~ public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } ~~~ 您還可以使用QueryString來傳遞操作方法的參數。例如,URL: *http://localhost:xxxxx/Movies/Edit?ID=4*還會將參數`ID`為 4的請求傳遞給`Movies`控制器的`Edit`操作方法。 [![clip_image003](https://box.kancloud.cn/2016-08-08_57a81c6808c9c.png "clip_image003")](http://images.cnitblog.com/blog/139239/201301/24114516-1cf474c4e51249f381f7fbd3b3c136dc.png) 打開`Movies`控制器。如下所示的兩個`Edit`操作方法。 ~~~ // // GET: /Movies/Edit/5 public ActionResult Edit(int id = 0) { Movie movie = db.Movies.Find(id); if (movie == null) { return HttpNotFound(); } return View(movie); } // // POST: /Movies/Edit/5 [HttpPost] public ActionResult Edit(Movie movie) { if (ModelState.IsValid) { db.Entry(movie).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(movie); } ~~~ 注意,第二個`Edit`操作方法的上面有[HttpPost](http://msdn.microsoft.com/en-us/library/system.web.mvc.httppostattribute.aspx)屬性。此屬性指定了`Edit`方法的重載,此方法僅被POST 請求所調用。您可以將[HttpGet](http://msdn.microsoft.com/en-us/library/system.web.mvc.httpgetattribute.aspx)屬性應用于第一個編輯方法,但這是不必要的,因為它是默認的屬性。(操作方法會被隱式的指定為`HttpGet`屬性,從而作為`HttpGet`方法。) `HttpGet``Edit`方法會獲取電影ID參數、 查找影片使用Entity Framework 的`Find`方法,并返回到選定影片的編輯視圖。如果不帶參數調用`Edit` 方法,ID 參數被指定為[默認值](http://msdn.microsoft.com/en-us/library/dd264739.aspx) 零。如果找不到一部電影,則返回[HttpNotFound](http://msdn.microsoft.com/en-us/library/gg453938(VS.98).aspx) 。當VS自動創建編輯視圖時,它會查看`Movie`類并為類的每個屬性創建用于Render的`<label>`和`<input>`的元素。下面的示例為自動創建的編輯視圖: ~~~ @model MvcMovie.Models.Movie @{ ViewBag.Title = "Edit"; } <h2>Edit</h2> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>Movie</legend> @Html.HiddenFor(model => model.ID) <div class="editor-label"> @Html.LabelFor(model => model.Title) </div> <div class="editor-field"> @Html.EditorFor(model => model.Title) @Html.ValidationMessageFor(model => model.Title) </div> <div class="editor-label"> @Html.LabelFor(model => model.ReleaseDate) </div> <div class="editor-field"> @Html.EditorFor(model => model.ReleaseDate) @Html.ValidationMessageFor(model => model.ReleaseDate) </div> <div class="editor-label"> @Html.LabelFor(model => model.Genre) </div> <div class="editor-field"> @Html.EditorFor(model => model.Genre) @Html.ValidationMessageFor(model => model.Genre) </div> <div class="editor-label"> @Html.LabelFor(model => model.Price) </div> <div class="editor-field"> @Html.EditorFor(model => model.Price) @Html.ValidationMessageFor(model => model.Price) </div> <p> <input type="submit" value="Save" /> </p> </fieldset> } <div> @Html.ActionLink("Back to List", "Index") </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") } ~~~ 注意,視圖模板在文件的頂部有 `@model MvcMovie.Models.Movie `的聲明,這將指定視圖期望的模型類型為`Movie`。 自動生成的代碼,使用了Helper方法的幾種簡化的 HTML 標記。[`Html.LabelFor`](http://msdn.microsoft.com/en-us/library/gg401864(VS.98).aspx)用來顯示字段的名稱("Title"、"ReleaseDate"、"Genre"或"Price")。[`Html.EditorFor`](http://msdn.microsoft.com/en-us/library/system.web.mvc.html.editorextensions.editorfor(VS.98).aspx)用來呈現 HTML `<input>`元素。[`Html.ValidationMessageFor`](http://msdn.microsoft.com/en-us/library/system.web.mvc.html.validationextensions.validationmessagefor(VS.98).aspx)用來顯示與該屬性相關聯的任何驗證消息。 運行該應用程序,然后瀏覽URL,/Movies。單擊**Edit**鏈接。在瀏覽器中查看頁面源代碼。HTML Form中的元素如下所示: ~~~ <form action="/Movies/Edit/4" method="post"> <fieldset> <legend>Movie</legend> <input data-val="true" data-val-number="The field ID must be a number." data-val-required="The ID field is required." id="ID" name="ID" type="hidden" value="4" /> <div class="editor-label"> <label for="Title">Title</label> </div> <div class="editor-field"> <input class="text-box single-line" id="Title" name="Title" type="text" value="Rio Bravo" /> <span class="field-validation-valid" data-valmsg-for="Title" data-valmsg-replace="true"></span> </div> <div class="editor-label"> <label for="ReleaseDate">ReleaseDate</label> </div> <div class="editor-field"> <input class="text-box single-line" data-val="true" data-val-date="The field ReleaseDate must be a date." data-val-required="The ReleaseDate field is required." id="ReleaseDate" name="ReleaseDate" type="text" value="4/15/1959 12:00:00 AM" /> <span class="field-validation-valid" data-valmsg-for="ReleaseDate" data-valmsg-replace="true"></span> </div> <div class="editor-label"> <label for="Genre">Genre</label> </div> <div class="editor-field"> <input class="text-box single-line" id="Genre" name="Genre" type="text" value="Western" /> <span class="field-validation-valid" data-valmsg-for="Genre" data-valmsg-replace="true"></span> </div> <div class="editor-label"> <label for="Price">Price</label> </div> <div class="editor-field"> <input class="text-box single-line" data-val="true" data-val-number="The field Price must be a number." data-val-required="The Price field is required." id="Price" name="Price" type="text" value="2.99" /> <span class="field-validation-valid" data-valmsg-for="Price" data-valmsg-replace="true"></span> </div> <p> <input type="submit" value="Save" /> </p> </fieldset> </form> ~~~ 被`<form>` HTML 元素所包括的 `<input>` 元素會被發送到,form的`action`屬性所設置的URL:/Movies/Edit。單擊**Edit**按鈕時,from數據將會被發送到服務器。 #### 處理 POST 請求 下面的代碼顯示了`Edit`操作方法的`HttpPost`處理: ~~~ [HttpPost] public ActionResult Edit(Movie movie) { if (ModelState.IsValid) { db.Entry(movie).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(movie); } ~~~ [ASP.NET MVC 模型綁定](http://msdn.microsoft.com/en-us/library/dd410405.aspx) 接收form所post的數據,并轉換所接收的`movie`請求數據從而創建一個`Movie`對象。`ModelState.IsValid`方法用于驗證提交的表單數據是否可用于修改(編輯或更新)一個`Movie`對象。如果數據是有效的電影數據,將保存到數據庫的`Movies`集合`(MovieDBContext` instance)。通過調用`MovieDBContext`的`SaveChanges`方法,新的電影數據會被保存到數據庫。數據保存之后,代碼會把用戶重定向到`MoviesController`類的`Index`操作方法,頁面將顯示電影列表,同時包括剛剛所做的更新。 如果form發送的值不是有效的值,它們將重新顯示在form中。*Edit.cshtml*視圖模板中的`Html.ValidationMessageFor` Helper將用來顯示相應的錯誤消息。 [![clip_image004](https://box.kancloud.cn/2016-08-08_57a81c648fd7c.png "clip_image004")](http://images.cnitblog.com/blog/139239/201301/24114518-359c45bd853f4b9aa4e0fb02004da12e.png) **注意**,為了使jQuery支持使用逗號的非英語區域的驗證 ,需要設置逗號(",")來表示小數點,你需要引入*globalize.js*并且你還需要具體的指定*cultures/globalize.cultures.js*文件 (地址在[https://github.com/jquery/globalize](https://github.com/jquery/globalize)) 在 JavaScript 中可以使用`Globalize.parseFloat`。下面的代碼展示了在"FR-FR" Culture下的 Views\Movies\Edit.cshtml 視圖: ~~~ @section Scripts { @Scripts.Render("~/bundles/jqueryval") <script src="~/Scripts/globalize.js"></script> <script src="~/Scripts/globalize.culture.fr-FR.js"></script> <script> $.validator.methods.number = function (value, element) { return this.optional(element) || !isNaN(Globalize.parseFloat(value)); } $(document).ready(function () { Globalize.culture('fr-FR'); }); </script> <script> jQuery.extend(jQuery.validator.methods, { range: function (value, element, param) { //Use the Globalization plugin to parse the value var val = $.global.parseFloat(value); return this.optional(element) || ( val >= param[0] && val <= param[1]); } }); </script> } ~~~ 十進制字段可能需要逗號,而不是小數點。作為臨時的修復,您可以向項目根 web.config 文件添加的全球化設置。下面的代碼演示設置為美國英語的全球化文化設置。 ~~~ <system.web> ??? <globalization culture ="en-US" /> ??? <!--elements removed for clarity--> </system.web> ~~~ 所有`HttpGet`方法都遵循類似的模式。它們獲取影片對象 (或對象集合,如`Index`里的對象集合),并將模型傳遞給視圖。`Create`方法將一個空的Movie對象傳遞給創建視圖。創建、 編輯、 刪除或以其它方式修改數據的方法都是`HttpPost`方法。使用HTTP GET 方法來修改數據是存在安全風險,在[ASP.NET MVC Tip #46 – Don’t use Delete Links because they create Security Holes](http://stephenwalther.com/blog/archive/2009/01/21/asp.net-mvc-tip-46-ndash-donrsquot-use-delete-links-because.aspx)的Blog中有完整的敘述。在 GET 方法中修改數據還違反了 HTTP 的最佳做法和[Rest](http://en.wikipedia.org/wiki/Representational_State_Transfer)架構模式, GET 請求不應更改應用程序的狀態。換句話說,執行 GET 操作,應該是一種安全的操作,沒有任何副作用,不會修改您持久化的數據。 #### 添加一個搜索方法和搜索視圖 在本節中,您將添加一個搜索電影流派或名稱的`SearchIndex`操作方法。這將可使用*/Movies/SearchIndex* URL。該請求將顯示一個 HTML 表單,其中包含輸入的元素,用戶可以輸入一部要搜索的電影。當用戶提交窗體時,操作方法將獲取用戶輸入的搜索條件并在數據庫中搜索。 #### 顯示 SearchIndex 窗體 通過將`SearchIndex`操作方法添加到現有的`MoviesController`類開始。該方法將返回一個視圖包含一個 HTML 表單。如下代碼: ~~~ public ActionResult SearchIndex(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); } ~~~ `SearchIndex`方法的第一行創建以下的[LINQ](http://msdn.microsoft.com/en-us/library/bb397926.aspx)查詢,以選擇看電影: ~~~ var movies = from m in db.Movies select m; ~~~ 查詢在這一點上,只是定義,并還沒有執行到數據上。 如果`searchString`參數包含一個字符串,可以使用下面的代碼,修改電影查詢要篩選的搜索字符串: ~~~ if (!String.IsNullOrEmpty(searchString)) { movies = movies.Where(s => s.Title.Contains(searchString)); } ~~~ 上面`s => s.Title` 代碼是一個[Lambda 表達式](http://msdn.microsoft.com/en-us/library/bb397687.aspx)。Lambda 是基于方法的[LINQ](http://msdn.microsoft.com/en-us/library/bb397926.aspx)查詢,(例如上面的where查詢)在上面的代碼中使用了標準查詢參數運算符的方法。當定義LINQ查詢或修改查詢條件時(如調用`Where` 或`OrderBy`方法時,不會執行 LINQ 查詢。相反,查詢執行會被延遲,這意味著表達式的計算延遲,直到取得實際的值或調用[`ToList`](http://msdn.microsoft.com/en-us/library/bb342261.aspx)方法。在`SearchIndex`示例中,SearchIndex 視圖中執行查詢。有關延遲的查詢執行的詳細信息,請參閱[Query Execution](http://msdn.microsoft.com/en-us/library/bb738633.aspx). 現在,您可以實現`SearchIndex`視圖并將其顯示給用戶。在`SearchIndex`方法內單擊右鍵,然后單擊**添加視圖**。在**添加視圖**對話框中,指定你要將`Movie`對象傳遞給視圖模板作為其模型類。在**框架模板**列表中,選擇**列表**,然后單擊**添加**. [![clip_image005](https://box.kancloud.cn/2016-08-08_57a81c682ad9f.png "clip_image005")](http://images.cnitblog.com/blog/139239/201301/24114518-b3cade1c9baa47af9abdc7eaf2861390.png) 當您單擊**添加**按鈕時,創建了*Views\Movies\SearchIndex.cshtml*視圖模板。因為你選中了**框架模板**的列表,Visual Studio 將自動生成**列表**視圖中的某些默認標記。框架模版創建了 HTML 表單。它會檢查`Movie`類,并為類的每個屬性創建用來展示的`<label>`元素。下面是生成的視圖: ~~~ @model IEnumerable<MvcMovie.Models.Movie> @{ ViewBag.Title = "SearchIndex"; } <h2>SearchIndex</h2> <p> @Html.ActionLink("Create New", "Create") </p> <table> <tr> <th> Title </th> <th> ReleaseDate </th> <th> Genre </th> <th> Price </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Title) </td> <td> @Html.DisplayFor(modelItem => item.ReleaseDate) </td> <td> @Html.DisplayFor(modelItem => item.Genre) </td> <td> @Html.DisplayFor(modelItem => item.Price) </td> <td> @Html.ActionLink("Edit", "Edit", new { id=item.ID }) | @Html.ActionLink("Details", "Details", new { id=item.ID }) | @Html.ActionLink("Delete", "Delete", new { id=item.ID }) </td> </tr> } </table> ~~~ 運行該應用程序,然后轉到 */Movies/SearchIndex*。追加查詢字符串到URL如`?searchString=ghost`。顯示已篩選的電影。 [![clip_image006](https://box.kancloud.cn/2016-08-08_57a81c6842533.png "clip_image006")](http://images.cnitblog.com/blog/139239/201301/24114519-ffa602bb3ea34359a79433d7609766f5.png) 如果您更改`SearchIndex`方法的簽名,改為參數`id`,在*Global.asax*文件中設置的默認路由將使得:`id`參數將匹配`{id}`占位符。 ~~~ {controller}/{action}/{id} ~~~ 原來的`SearchIndex`方法看起來是這樣的: ~~~ public ActionResult SearchIndex(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); } ~~~ 修改后的`SearchIndex`方法將如下所示: ~~~ public ActionResult SearchIndex(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); } ~~~ 您現在可以將搜索標題作為路由數據 (部分URL) 來替代QueryString。 [![clip_image007](https://box.kancloud.cn/2016-08-08_57a81c685a914.png "clip_image007")](http://images.cnitblog.com/blog/139239/201301/24114521-edba861dca8e48edb2a04ca49ca5b842.png) 但是,每次用戶想要搜索一部電影時, 你不能指望用戶去修改 URL。所以,現在您將添加 UI頁面,以幫助他們去篩選電影。如果您更改了的`SearchIndex`方法來測試如何傳遞路由綁定的 ID 參數,更改它,以便您的`SearchIndex`方法采用字符串`searchString`參數: ~~~ public ActionResult SearchIndex(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\SearchIndex.cshtml*文件,并在 `@Html.ActionLink("Create New", "Create")`后面,添加以下內容: ~~~ @using (Html.BeginForm()){ <p> Title: @Html.TextBox("SearchString")<br /> <input type="submit" value="Filter" /></p> } ~~~ 下面的示例展示了添加后, *Views\Movies\SearchIndex.cshtml *文件的一部分: ~~~ @model IEnumerable<MvcMovie.Models.Movie> @{ ViewBag.Title = "SearchIndex"; } <h2>SearchIndex</h2> <p> @Html.ActionLink("Create New", "Create") @using (Html.BeginForm()){ <p> Title: @Html.TextBox("SearchString") <br /> <input type="submit" value="Filter" /></p> } </p> ~~~ `Html.BeginForm Helper`創建開放`<form>`標記。`Html.BeginForm`Helper將使得, 在用戶通過單擊**篩選**按鈕提交窗體時,窗體Post本Url。運行該應用程序,請嘗試搜索一部電影。 [![clip_image008](https://box.kancloud.cn/2016-08-08_57a81c6870a1d.png "clip_image008")](http://images.cnitblog.com/blog/139239/201301/24114522-d16ba1fb2eb8420db76eb1138bdaa469.png) `SearchIndex`沒有`HttpPost` 的重載方法。你并不需要它,因為該方法并不更改應用程序數據的狀態,只是篩選數據。 您可以添加如下的`HttpPost SearchIndex` 方法。在這種情況下,請求將進入`HttpPost SearchIndex`方法,```HttpPost SearchIndex`方法將返回如下圖的內容。 ~~~ [HttpPost] public string SearchIndex(FormCollection fc, string searchString) { return "<h3> From [HttpPost]SearchIndex: " + searchString + "</h3>"; } ~~~ [![clip_image009](https://box.kancloud.cn/2016-08-08_57a81c688f6a3.png "clip_image009")](http://images.cnitblog.com/blog/139239/201301/24114523-efd3b18a9d3f4ad4b9109a6b63467f33.png) 但是,即使您添加此`HttpPost``SearchIndex` 方法,這一實現其實是有局限的。想象一下您想要添加書簽給特定的搜索,或者您想要把搜索鏈接發送給朋友們,他們可以通過單擊看到一樣的電影搜索列表。請注意 HTTP POST 請求的 URL 和GET 請求的URL 是相同的(localhost:xxxxx/電影/SearchIndex)— — 在 URL 中沒有搜索信息。現在,搜索字符串信息作為窗體字段值,發送到服務器。這意味著您不能在 URL 中捕獲此搜索信息,以添加書簽或發送給朋友。 解決方法是使用重載的`BeginForm` ,它指定 POST 請求應添加到 URL 的搜索信息,并應該路由到 HttpGet `SearchIndex` 方法。將現有的無參數`BeginForm` 方法,修改為以下內容: ~~~ @using (Html.BeginForm("SearchIndex","Movies",FormMethod.Get)) ~~~ [![clip_image010](https://box.kancloud.cn/2016-08-08_57a81c68a27b5.png "clip_image010")](http://images.cnitblog.com/blog/139239/201301/24114524-84a4acb99a98452f9d9860c8b16a1846.png) 現在當您提交搜索,該 URL 將包含搜索的查詢字符串。搜索還會請求到 `HttpGet SearchIndex`操作方法,即使您也有一個`HttpPost SearchIndex`方法。 [![clip_image011](https://box.kancloud.cn/2016-08-08_57a81c68c0fa6.png "clip_image011")](http://images.cnitblog.com/blog/139239/201301/24114525-dbb9153aab25456488fbf6e9459b5cda.png) #### 按照電影流派添加搜索 如果您添加了`HttpPost `的`SearchIndex`方法,請立即刪除它。 接下來,您將添加功能可以讓用戶按流派搜索電影。將`SearchIndex`方法替換成下面的代碼: ~~~ public ActionResult SearchIndex(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)) return View(movies); else { return View(movies.Where(x => x.Genre == movieGenre)); } } ~~~ 這版的`SearchIndex`方法將接受一個附加的`movieGenre`參數。前幾行的代碼會創建一個`List`對象來保存數據庫中的電影流派。 下面的代碼是從數據庫中檢索所有流派的 LINQ 查詢。 ~~~ var GenreQry = from d in db.Movies orderby d.Genre select d.Genre; ~~~ 該代碼使用泛型 [List](http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx)集合的[AddRange](http://msdn.microsoft.com/en-us/library/z883w3dc.aspx)方法將所有不同的流派,添加到集合中的。(使用`Distinct`修飾符,不會添加重復的流派 -- 例如,在我們的示例中添加了兩次喜劇)。該代碼然后在`ViewBag`對象中存儲了流派的數據列表。 下面的代碼演示如何檢查`movieGenre`參數。如果它不是空的,代碼進一步指定了所查詢的電影流派。 ~~~ if (string.IsNullOrEmpty(movieGenre)) return View(movies); else { return View(movies.Where(x => x.Genre == movieGenre)); } ~~~ #### 在SearchIndex 視圖中添加選擇框支持按流派搜索 在`TextBox `Helper之前添加 `Html.DropDownList `Helper到*Views\Movies\SearchIndex.cshtml*文件中。添加完成后,如下面所示: ~~~ <p> @Html.ActionLink("Create New", "Create") @using (Html.BeginForm("SearchIndex","Movies",FormMethod.Get)){ <p>Genre: @Html.DropDownList("movieGenre", "All") Title: @Html.TextBox("SearchString") <input type="submit" value="Filter" /></p> } </p> ~~~ 運行該應用程序并瀏覽 */Movies/SearchIndex*。按流派、 按電影名,或者同時這兩者,來嘗試搜索。 [![clip_image012](https://box.kancloud.cn/2016-08-08_57a81c62ec336.png "clip_image012")](http://images.cnitblog.com/blog/139239/201301/24114527-b663f51a97424a4db0467ce297400b43.png) 在這一節中您修改了CRUD 操作方法和框架所生成的視圖。您創建了一個搜索操作方法和視圖,讓用戶可以搜索電影標題和流派。在下一節中,您將看到如何將屬性添加到`Movie`模型,以及如何添加一個初始設定并自動創建一個測試數據庫。 -------------------------------------------------------------------------------------------------------------------- 譯者注: 本系列共9篇文章,翻譯自Asp.Net MVC4 官方教程,由于本系列文章言簡意賅,篇幅適中,從一個示例開始講解,全文最終完成了一個管理影片的小系統,非常適合新手入門Asp.Net MVC4,并由此開始開發工作。9篇文章為: 1. Asp.Net MVC4 入門介紹 · 原文地址:[http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4](http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4) · 譯文地址:[http://www.cnblogs.com/powertoolsteam/archive/2012/11/01/2749906.html](http://www.cnblogs.com/powertoolsteam/archive/2012/11/01/2749906.html) 2. 添加一個控制器 · 原文地址:[http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-controller](http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-controller) · 譯文地址:[http://www.cnblogs.com/powertoolsteam/archive/2012/11/02/2751015.html](http://www.cnblogs.com/powertoolsteam/archive/2012/11/02/2751015.html) 3. 添加一個視圖 · 原文地址:[http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-view](http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-view) · 譯文地址:[http://www.cnblogs.com/powertoolsteam/archive/2012/11/06/2756711.html](http://www.cnblogs.com/powertoolsteam/archive/2012/11/06/2756711.html) 4. 添加一個模型 · 原文地址:[http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-model](http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-model) · 譯文地址:[http://www.cnblogs.com/powertoolsteam/archive/2012/12/17/2821495.html](http://www.cnblogs.com/powertoolsteam/archive/2012/12/17/2821495.html) 5. 從控制器訪問數據模型 · 原文地址:[http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/accessing-your-models-data-from-a-controller](http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/accessing-your-models-data-from-a-controller) · 譯文地址:[http://www.cnblogs.com/powertoolsteam/archive/2013/01/11/2855935.html](http://www.cnblogs.com/powertoolsteam/archive/2013/01/11/2855935.html) 6. 驗證編輯方法和編輯視圖 · 原文地址:[http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-edit-methods-and-edit-view](http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-edit-methods-and-edit-view) · 譯文地址:http://www.cnblogs.com/powertoolsteam/archive/2013/01/24/2874622.html 7. 給電影表和模型添加新字段 · 原文地址:[http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-new-field-to-the-movie-model-and-table](http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-new-field-to-the-movie-model-and-table) · 譯文地址: 8. 給數據模型添加校驗器 · 原文地址:[http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-validation-to-the-model](http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-validation-to-the-model) · 譯文地址: 9. 查詢詳細信息和刪除記錄 · 原文地址:[http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-details-and-delete-methods](http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-details-and-delete-methods) · 譯文地址:
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看