如何在 ASP.NET Core MVC 中使用 "int" 的搜索功能?
How to use seach function by "int" in ASP.NET Core MVC?
我用这种方式添加了搜索功能。
但它只能按字符串搜索。
我想通过“int”添加一个搜索功能。
有人有建议吗?
public async Task<IActionResult> Index(string searchString)
{
var movies = from m in _context.Movie
select m;
if (!String.IsNullOrEmpty(searchString))
{
movies = movies.Where(s => s.Title.Contains(searchString));
}
return View(await movies.ToListAsync());
}
这是我的资源
https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/search?view=aspnetcore-5.0
我也试过这样,但似乎没有用
通过Int
参数搜索值,类似于字符串搜索。在视图页面中,将元素的名称属性设置为与操作方法中的参数名称相同。
您可以查看以下示例:
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
Find by name: <input type="text" name="searchString" value="@ViewData["CurrentFilter"]" />
Find by ID: <input type="text" name="searchId" value="@ViewData["CurrentID"]" />
<input type="submit" value="Search" class="btn btn-default" /> |
<a asp-action="Index">Back to Full List</a>
</p>
</div>
</form>
控制器:
public async Task<IActionResult> Index(string searchString, int searchId)
{
var students = from stu in _context.Students
select stu;
//if (!String.IsNullOrEmpty(searchString))
//{
// students = students.Where(s => s.LastName.Contains(searchString.ToString()));
//}
if (searchId !=0)
{
students = students.Where(s => s.ID == searchId );
}
ViewData["CurrentFilter"] = searchString;
ViewData["CurrentID"] = searchId;
return View(await students.ToListAsync());
}
那么,结果如下:
我用这种方式添加了搜索功能。 但它只能按字符串搜索。 我想通过“int”添加一个搜索功能。 有人有建议吗?
public async Task<IActionResult> Index(string searchString)
{
var movies = from m in _context.Movie
select m;
if (!String.IsNullOrEmpty(searchString))
{
movies = movies.Where(s => s.Title.Contains(searchString));
}
return View(await movies.ToListAsync());
}
这是我的资源
https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/search?view=aspnetcore-5.0
我也试过这样,但似乎没有用
通过Int
参数搜索值,类似于字符串搜索。在视图页面中,将元素的名称属性设置为与操作方法中的参数名称相同。
您可以查看以下示例:
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
Find by name: <input type="text" name="searchString" value="@ViewData["CurrentFilter"]" />
Find by ID: <input type="text" name="searchId" value="@ViewData["CurrentID"]" />
<input type="submit" value="Search" class="btn btn-default" /> |
<a asp-action="Index">Back to Full List</a>
</p>
</div>
</form>
控制器:
public async Task<IActionResult> Index(string searchString, int searchId)
{
var students = from stu in _context.Students
select stu;
//if (!String.IsNullOrEmpty(searchString))
//{
// students = students.Where(s => s.LastName.Contains(searchString.ToString()));
//}
if (searchId !=0)
{
students = students.Where(s => s.ID == searchId );
}
ViewData["CurrentFilter"] = searchString;
ViewData["CurrentID"] = searchId;
return View(await students.ToListAsync());
}
那么,结果如下: