Learn the ASP. NET Core Razor Programming series Catalog
Learn ASP. NET Core Razor programming series One
Learn ASP. NET Core Razor Programming series two--Add an entity
Learn ASP. NET Core Razor Programming series three--Create a data table and create a project basic page
Learn ASP. Razor Programming Series four--asp.net core Razor list template page
Learn ASP. Razor Programming series five--asp.net core Razor new Template page
Learn ASP. NET Core Razor Programming series six--database initialization
Learn ASP. NET Core Razor Programming series seven--Modify List page
Learn ASP. NET Core Razor Programming series eight--concurrent processing
In this tutorial we learn how to add query functionality, and we will be adding search functionality to the book list page by searching for books by "book Name" or "author".
first, according to the book name query
Use the following code to modify the Ongetasync method of the book List page:
Public AsyncTask Ongetasync (stringsearchstring) { varBooks = fromMinch_context. BookSelectm; if(!String.IsNullOrEmpty (searchstring)) {Books= books. Where (s = =s.name.contains (searchstring)); } Book=awaitBooks. Tolistasync (); //Book = await _context. Book.tolistasync ();}
The first line of the Ongetasync method in the preceding code creates a LINQ query for selecting books:
var from inch _context. Book select m;
This line of code simply defines the query and has not been passed to the database for execution.
If the searchstring parameter contains a query condition string, the query statement is modified according to the query criteria string, as follows:
if (! String.IsNullOrEmpty (searchstring)) { = books. Where (s = = S.name.contains (searchstring)); }
s = = S.title.contains () This code is a LAMBDA expression. LAMBDA is used as a parameter in a method-based LINQ query as a standard query operator method, such as the Where method or Contains (as in the preceding code). After a LINQ query is defined or modified by a calling method (such as where, Contains, or by), the query statement is not executed immediately and is deferred. This means that the evaluation of an expression is deferred until the value of its implementation is cycled through or the Tolistasync method is called.
Note: The Contains method runs in the database, not in C # code. Whether the query criteria are case sensitive depends on what kind of database you are using or what sort of collation you use in the database. On SQL Server, Contains maps to like in database query syntax, which is case insensitive. In SQLite, if you use the default collation, the query criteria are case-sensitive.
Open the Book list page in the browser and add the following string to the URL of the browser's address bar: Searchstring=java (e.g. Http://localhost:53416/Books?searchString=Java). The book Management system displays a list of books based on the query criteria you enter. 1 is a list of books without a query, and all of the book information is displayed. In the figure 2, a list of book information is displayed that is the query condition for Java.
If the following route template is added to the book List page, the query criteria string can be passed to the background as a paragraph in the URL address (for example, Http://localhost:53416/Books/Java).
@page "{searchstring}"
The above routing constraint code allows the query condition to be placed in the URL as a URL of a piece of data to query the book information. "{searchstring?}" In the? Indicates that this is an optional route parameter.
However, the scheme of searching for book information by modifying the URL is very unfriendly to the user. Therefore, in the following steps, we will add UI interface to facilitate users to query the book information. If you have added the routing constraint "{searchstring?}" to the index.cshtml page, please remove it first.
Open the pages/books/index.cshtml file in Solution Explorer in Visual Studio 2017 and add a form form to the file with the following code:
@page @model razormvcbooks.pages.books.indexmodel@{viewdata["Title"] = "Index";}<H2>Index</H2> <P> <aAsp-page= "Create">Create New</a></P><form> <P>Book name:<inputtype= "text"name= "SearchString"> <inputtype= "Submit"value= "Filter" /> </P></form><Tableclass= "Table">
1. After making the above code modification, browsing in the browser may appear garbled, such as.
2. For the Chinese garbled solution is to open the index.cshtml file in Visual Studio 2017, in the menu select File à "index.cshtml Save As ..." menu. Such as.
3. In the pop-up dialog, "Save" button, click the drop-down arrow with the mouse, and then select "Encode save ..." in the pop-up menu. Such as.
4. In the pop-up dialog, we see that the page is saved by using the "GB2312" encoding by default, and we select "UTF-8" to save the code. Operations such as.
5. Press F5 in the browser to refresh, and then we see the Chinese garbled. Such as.
6. In the Book Name query input box, enter "SQL" and then click the "Query" button, the system will display the corresponding book information according to the query criteria. The query criteria is automatically added to the URL, as in 1. The results of the query based on the query criteria are 2 places.
Second, according to the author query
Locate the pages/books/index.cshtml.cs file in Solution Explorer in Visual Studio 2017 and add two variables with a property, as shown in the code below.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Threading.Tasks;usingMICROSOFT.ASPNETCORE.MVC;usingMicrosoft.AspNetCore.Mvc.RazorPages;usingMicrosoft.entityframeworkcore;usingRazormvcbooks.models;usingMicrosoft.AspNetCore.Mvc.Rendering;namespacerazormvcbooks.pages.books{ Public classIndexmodel:pagemodel {Private ReadOnlyRazorMvcBooks.Models.BookContext _context; PublicIndexmodel (RazorMvcBooks.Models.BookContext context) {_context=context; } PublicList<book>Books; PublicSelectList Authors; Public stringAuthor {Get;Set; }
SelectList Author contains a list of authors that allow users to select an author on the interface.
The Author property contains the specific author selected by the user (for example, "WU").
We make the following modifications to the Ongetasync method.
Public AsyncTask Ongetasync (stringAuthorstringsearchstring) {IQueryable<string> authorquery = fromMinch_context. Book byM.authorSelectM.author; varBooks = fromMinch_context. BookSelectm; if(!String.IsNullOrEmpty (searchstring)) {Books= books. Where (s = =s.name.contains (searchstring)); } if(!String.IsNullOrEmpty (author)) {Books= books. Where (x = X.author = =author); } Authors=NewSelectList (awaitauthorquery.distinct (). Tolistasync ()); Book=awaitBooks. Tolistasync (); //Book = await _context. Book.tolistasync ();}
The following code is a statement of a LINQ query that can query all authors from the database.
iqueryable<string from in _context. Book m.author select m.author;
The author list selectlist is created by using the following statement.
New SelectList (await authorquery.distinct (). Tolistasync ());
Second, add the author query conditions
found in Solution Explorer in Visual Studio 2017 index.cshtml file, and make the following changes, the code is as follows:
@page @model RazorMvcBooks.Pages.Books.IndexModel @{viewdata["Title"] = "Index";}<H2>Index</H2> <P> <aAsp-page= "Create">Create New</a></P><form> <P> <Selectasp-for= "Author"Asp-items= "Model.authors"> <optionvalue="">All</option> </Select>Book name<inputtype= "text"name= "SearchString"> <inputtype= "Submit"value= "Query" /> </P></form> <Tableclass= "Table">
Through the browser, the reference according to the book Name query operation, to test by the author to query. Such as.
Learn ASP. NET Core Razor programming series nine--Add query function