https://docs.asp.net/en/latest/tutorials/first-mvc-app/search.html
In this section you’ll add search capability to the Index
action method that lets you search movies by genre or name.
Update the Index
action method to enable search:
public async Task
{
var movies = _context.Movie.Select(x => x);
if (string.IsNullOrEmpty(searchString) == false)
{
movies = movies.Where(x => x.Title.Contains(searchString));
}
return View(await movies.ToListAsync());
}
上面第一行代码,还可以改为
var movies = from m in _context.Movie
select m;
The first line of the Index
action method creates a LINQ query to select the movies:
The query is only defined at this point, it has not been run against the database.
If the searchString
parameter contains a string, the movies query is modified to filter on the value of the search string, using the following code:
if (string.IsNullOrEmpty(searchString) == false)
{
movies = movies.Where(x => x.Title.Contains(searchString));
}
The x => x.Title.Contains()
code above is a Lambda Expression.
Lambdas are used in method-based LINQ queries as arguments to standard query operator methods such as the Where method or Contains
used in the code above.
LINQ queries are not executed when they are defined or when they are modified by calling a method such as Where
, Contains
or OrderBy
.
Instead, query execution is deferred, which means that the evaluation of an expression is delayed until its realized value is actually iterated over or the ToListAsync
method is called.
For more information about deferred query execution, see Query Execution. //延迟加载
Note:
The Contains method is run on the database, not the c# code above.
On the database, Containsmaps to SQL LIKE, which is case insensitive. //不区分大小写
Navigate to /Movies/Index
. Append a query string such as ?searchString=ghost
to the URL. The filtered movies are displayed.
If you change the signature of the Index
method to have a parameter named id
, the id
parameter will match the optional {id}
placeholder for the default routes set in Startup.cs.
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
You can quickly rename the searchString
parameter to id
with the rename command.
Right click on searchString
> Rename.
Change the parameter to id
and all occurrences of searchString
change to id
.
You can now pass the search title as route data (a URL segment) instead of as a query string value.
However, you can’t expect users to modify the URL every time they want to search for a movie.
So now you’ll add UI to help them filter movies.
If you changed the signature of the Index
method to test how to pass the route-bound ID
parameter, change it back so that it takes a parameter named searchString
:
添加一个Filter进行过滤
Open the Views/Movies/Index.cshtml file, and add the <form>
markup highlighted below:
@{
ViewData["Title"] = "Index";
}
@Html.DisplayNameFor(model => model.Movies\[0\].Genre) | @Html.DisplayNameFor(model => model.Movies\[0\].Price) | @Html.DisplayNameFor(model => model.Movies\[0\].ReleaseDate) | @Html.DisplayNameFor(model => model.Movies\[0\].Title) | |
---|---|---|---|---|
@Html.DisplayFor(modelItem => item.Genre) | @Html.DisplayFor(modelItem => item.Price) | @Html.DisplayFor(modelItem => item.ReleaseDate) | @Html.DisplayFor(modelItem => item.Title) | Edit | Details | Delete |
Test the app by searching by genre, by movie title, and by both.
手机扫一扫
移动阅读更方便
你可能感兴趣的文章