<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12
      samesite


      【譯】MVC3 20個(gè)秘方-(10)根據(jù)關(guān)鍵字搜索

      問題

      當(dāng)排序和分頁(yè)和過濾都不夠幫用戶去找到他們想要的結(jié)果時(shí),想一個(gè)最好的備選方式是讓用戶輸入(關(guān)鍵字)他們想要什么。

      解決方案

      用HtmlHelper創(chuàng)建一個(gè)新的From和 文本輸入框,并且借助LINQ根據(jù)用戶輸入的關(guān)鍵字在之前過濾過的結(jié)果里查找。

      討論

      和前邊的秘方很像,添加一個(gè)根據(jù)keyword 搜索的功能需要更新 Book/Index view 和 BookController。在View里添加一個(gè)新的from和textbox 讓用戶輸入keyword。同時(shí)也要確保當(dāng)用戶改變排序規(guī)則、過濾、分頁(yè)時(shí),關(guān)鍵字保持。

      下邊的代碼是對(duì)View的更新:

      @model PagedList.IPagedList<MvcApplication.Models.Book>
      <h2>@MvcApplication4.Resources.Resource1.BookIndexTitle</h2>
      <p>
      @Html.ActionLink("Create New", "Create")
      </p>
      <p>
      Show:
      @if (ViewBag.CurrentFilter != "")
      {
      @Html.ActionLink("All", "Index", new
      {
      sortOrder = ViewBag.CurrentSortOrder,
      Keyword = ViewBag.CurrentKeyword
      })
      }
      else
      {
      @:All
      }
      &nbsp; | &nbsp;
      @if (ViewBag.CurrentFilter != "NewReleases")
      {
      @Html.ActionLink("New Releases", "Index", new
      {
      filter = "NewReleases",
      sortOrder = ViewBag.CurrentSortOrder,
      Keyword = ViewBag.CurrentKeyword
      })
      }
      else
      {
      @:New Releases
      }
      &nbsp; | &nbsp;
      @if (ViewBag.CurrentFilter != "ComingSoon")
      {
      @Html.ActionLink("Coming Soon", "Index", new
      {
      filter = "ComingSoon",
      sortOrder = ViewBag.CurrentSortOrder,
      Keyword = ViewBag.CurrentKeyword
      })
      }
      else
      {
      @:Coming Soon
      }
      </p>
      @using (Html.BeginForm())
      {
      @:Search: @Html.TextBox("Keyword")<input type="submit" value="Search" />

      }
      @Html.Partial("_Paging")
      <table>
      <tr>
      <th>
      @Html.ActionLink("Title", "Index", new
      {
      sortOrder = ViewBag.TitleSortParam,
      filter = ViewBag.CurrentFilter,
      Keyword = ViewBag.CurrentKeyword
      })
      </th>
      <th>
      @Html.ActionLink("Isbn", "Index", new
      {
      sortOrder = ViewBag.IsbnSortParam,
      filter = ViewBag.CurrentFilter,
      Keyword = ViewBag.CurrentKeyword
      })
      </th>
      <th>
      Summary
      </th>
      <th>
      @Html.ActionLink("Author", "Index", new
      {
      sortOrder = ViewBag.AuthorSortParam,
      filter = ViewBag.CurrentFilter,
      Keyword = ViewBag.CurrentKeyword
      })
      </th>
      <th>
      Thumbnail
      </th>
      <th>
      @Html.ActionLink("Price", "Index", new
      {
      sortOrder = ViewBag.PriceSortParam,
      filter = ViewBag.CurrentFilter,
      Keyword = ViewBag.CurrentKeyword
      })
      </th>
      <th>
      @Html.ActionLink("Published", "Index", new
      {
      sortOrder = ViewBag.PublishedSortParam,
      filter = ViewBag.CurrentFilter,
      Keyword = ViewBag.CurrentKeyword
      })
      </th>
      <th>
      </th>
      </tr>
      @foreach (var item in Model)
      {
      <tr>
      <td>
      @Html.DisplayFor(modelItem => item.Title)
      </td>
      <td>
      @Html.DisplayFor(modelItem => item.Isbn)
      </td>
      <td>
      @Html.DisplayFor(modelItem => item.Summary)
      </td>
      <td>
      @Html.DisplayFor(modelItem => item.Author)
      </td>
      <td>
      @Html.DisplayFor(modelItem => item.Thumbnail)
      </td>
      <td>
      @Html.DisplayFor(modelItem => item.Price)
      </td>
      <td>
      @Html.DisplayFor(modelItem => item.Published)
      </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>
      @Html.Partial("_Paging")

      最終,BookController 需要被更新,在下邊的例子,Index() action 更新

      using System;
      using System.Collections.Generic;
      using System.Data;
      using System.Data.Entity;
      using System.Linq;
      using System.Linq.Dynamic;
      using System.Web;
      using System.Web.Mvc;
      using MvcApplication.Models;
      using MvcApplication.Utils;
      using PagedList;
      namespace MvcApplication.Controllers
      {
      public class BooksController : Controller
      {
      private BookDBContext db = new BookDBContext();
      //
      // GET: /Books/
      public ViewResult Index(string sortOrder, string filter,string Keyword, int page = 1)
      {
      #region ViewBag Resources

      #endregion
      #region ViewBag Sort Params

      #endregion
      var books = from b in db.Books select b;
      #region Keyword Search
      if (!String.IsNullOrEmpty(Keyword))
      {
      books = books.Where(b =>
      b.Title.ToUpper().Contains(Keyword.ToUpper())
      || b.Author.ToUpper().Contains(
      Keyword.ToUpper()));
      }
      ViewBag.CurrentKeyword =
      String.IsNullOrEmpty(Keyword) ? "" : Keyword;
      #endregion
      #region Filter Switch

      #endregion
      int maxRecords = 1;
      int currentPage = page - 1;
      return View(books.ToPagedList(currentPage,
      maxRecords));
      }

      }
      }

      譯者:上邊代碼是以書名和作者名為搜索條件的。你也可以自己擴(kuò)展。比如根據(jù)ISBN:

      books = books.Where(b =>
      b.Title.ToUpper().Contains(Keyword.ToUpper())
      || b.Author.ToUpper().Contains(
      Keyword.ToUpper()||b.ISBN.ToUpper().Contains(
      Keyword.ToUpper()));

      當(dāng)然這樣做的話查詢效率會(huì)有問題。我們可以在UI提供一個(gè)dropdownlist 讓用戶去選擇根據(jù)什么條件去搜索關(guān)鍵字。






       



      posted @ 2011-11-30 17:57  技術(shù)弟弟  閱讀(2685)  評(píng)論(1)    收藏  舉報(bào)
      主站蜘蛛池模板: 午夜DY888国产精品影院| 国产精品夫妇激情啪发布| 黄色舔女人逼一区二区三区| 国产sm重味一区二区三区| 精品人妻久久久久久888| 亚洲成色av网站午夜影视| 欧美人与动牲交A免费观看| 日韩加勒比一本无码精品| 国产精品中文字幕自拍| 亚洲国产精品久久久久秋霞影院| 亚洲中文无码av永久不收费| 丰满人妻熟妇乱又仑精品| 亚洲粉嫩av一区二区黑人| av新版天堂在线观看| 国产一区二区亚洲精品| 亚洲熟女精品一区二区| 99精品热在线在线观看视| 国产va免费精品观看| 午夜精品福利亚洲国产| 极品少妇无套内射视频| 国产内射性高湖| 色九月亚洲综合网| av午夜福利一片免费看久久| 狠狠躁日日躁夜夜躁欧美老妇 | 亚洲国产韩国欧美在线| 国产精品户外野外| 凸凹人妻人人澡人人添| 九九热在线精品免费视频 | 日本一区二区三本视频在线观看| 欧美日本在线| 99久久精品国产一区二区蜜芽| 91人妻熟妇在线视频| 国产超碰无码最新上传| 国产久免费热视频在线观看| 新版天堂资源中文8在线| 日本一区二区中文字幕久久| 午夜av高清在线观看| 色综合久久综合欧美综合网| 99国产精品一区二区蜜臀| 欧美国产成人精品二区芒果视频 | 污网站在线观看视频|