20 secrets of MVC3-(10) search by keyword

Source: Internet
Author: User

 

Problem

When sorting, paging, and filtering are not enough to help users find the desired results, the best alternative is to let users enter (keyword) what they want.

Solution

Use HtmlHelper to create a new From and text input box, and use LINQ to search for the previously filtered results based on user input keywords.

Discussion

Similar to the recipe of the front edge, add a function to update Book/Index view and BookController Based on the keyword search function. Add a new from and textbox in the View to allow users to enter keyword. At the same time, make sure that the keywords are maintained when the user changes the sorting rules, filtering, and paging.

The following code updates the View:

@ Model PagedList. IPagedList <MvcApplication. Models. Book>

<H2> @ MvcApplication4.Resources. Resource1.BookIndexTitle

<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 ")

In the end, the BookController needs to be updated. In the following example, Index () action updates www.2cto.com.

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 ));

}

 

}

}

The above Code is based on the name of the title and the name of the author. You can also expand it by yourself. For example, according to ISBN:

Books = books. Where (B =>

B. Title. ToUpper (). Contains (Keyword. ToUpper ())

| B. Author. ToUpper (). Contains (

Keyword. ToUpper () | B. ISBN. ToUpper (). Contains (

Keyword. ToUpper ()));

Of course, the query efficiency will be problematic. We can provide a dropdownlist In the UI to allow users to select the criteria to search for keywords.

 

 

 


 

 

 

Author technical brother

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.