我知道简单的搜索结果如下:
<form asp-controller="User" asp-action="search">
<input type="text" name="search"/>
<button class="btn btn-primary" id="searchBtn">SÃK</button>
</form>
public class User : Controller
{
public IActionResult Search(string search = null)
{
IEnumerable<User> users;
if (!string.IsNullOrEmpty(search))
{
users = _users.GetAllUsers().Where(s => s.Email.ToLower().Contains(search.ToLower()));
}
else
{
users = _users.GetAllUsers();
}
return View("index", users);
}
}
但我想做的是添加指定搜索字段的功能,如:
或
按姓名
public IActionResult Search(string search = null, string field = null)
{
IEnumerable<User> users;
if (!string.IsNullOrEmpty(search))
{
if (!string.IsNullOrEmpty(field))
{
if(field == "ByEmail")
{
//I'll modify this to not get all rows from the database, but wrote it
//like that for simplicity
users = _users.GetAllUsers().Where(s => s.Email.ToLower().Contains(search.ToLower()));
}
else if(field =="ByName")
{
users = _users.GetAllUsers().Where(s => s.Name.ToLower().Contains(search.ToLower()));
}
}
else
{
users = _users.GetAllUsers();
}
else
{
users = _users.GetAllUsers();
}
return View("index", users);
}
非常感谢。