20 tips for ASP. net mvc 3 Development (10) [20 Recipes for Programming MVC 3]: List Search by keyword

Source: Internet
Author: User
Tags actionlink

Topics

When sorting, paging, and filtering cannot help the user find the desired content, the best way to next is to let the user enter the content he wants to search.

Solution

Use Html. Helper to create a form and text input box, and use Linq Libary to query the result based on the filter selection result using the keyword entered by the user.

Discussion

Just like the previous tips, adding a keyword search requires modifying the Books and Index views and the BooksController controller. Create a new form in the view and add a text box with the new keyword. In addition, in order to search for keywords, you need to re-edit the original code to keep the sorting and filter options selected by the user. The modified Book and Index views are as follows:

@model PagedList.IPagedList<MvcApplication4.Models.Book>           
<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")

You need to modify the paging view to maintain the current search keywords:

<p>
@if (Model.HasPreviousPage)
{
@Html.ActionLink("<< First", "Index", new {
page = 1,
sortOrder = ViewBag.CurrentSortOrder,
filter = ViewBag.CurrentFilter,
Keyword = ViewBag.CurrentKeyword })
@Html.Raw("&nbsp;");
@Html.ActionLink("< Prev", "Index", new {
page = Model.PageNumber - 1,
sortOrder = ViewBag.CurrentSortOrder,
filter = ViewBag.CurrentFilter,
Keyword = ViewBag.CurrentKeyword })
}
else
{
@:<< First
@Html.Raw("&nbsp;");
@:< Prev
}
&nbsp;&nbsp;
@if (Model.HasNextPage)
{
@Html.ActionLink("Next >", "Index", new {
page = Model.PageNumber + 1,
sortOrder = ViewBag.CurrentSortOrder,
filter = ViewBag.CurrentFilter,
Keyword = ViewBag.CurrentKeyword })
@Html.Raw("&nbsp;");
@Html.ActionLink("Last >>", "Index", new {
page = Model.PageCount,
sortOrder = ViewBag.CurrentSortOrder,
filter = ViewBag.CurrentFilter,
Keyword = ViewBag.CurrentKeyword })
}
else
{
@:Next >
@Html.Raw("&nbsp;")
@:Last >>
}
</p>

Finally, you need to modify the BooksController controller. In the following example, modify the Index () method and add a new input parameter. Users can enter keywords to search for book titles and authors. To add other fields for search, you only need to modify the following example, including the additional fields:

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 MvcApplication4.Models;
using MvcApplication4.Utils;
using PagedList;

namespace MvcApplication4.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));
}
...
}

}

Reference

Original book Address Book source code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.