使用 Asp.net 核心 mvc 的具有多个搜索选项的搜索功能
Search functionality with multi search options using Asp.net core mvc
我正在尝试向我的项目中的搜索功能添加多个选项。
我知道简单的搜索就像:
<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);
}
}
但我想做的是添加指定搜索字段的功能,例如:电子邮件地址或按名称。
我想要实现的是添加一个 DropDownList 以添加选择要查看的字段的能力。操作类似于:
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);
}
但是如何在视图中实现这个场景呢?
如何通过视图中的表单将多个参数传递给控制器?
谢谢。
没关系,我想通了。
视图将像:
@using (Html.BeginForm("Search", "User"))
{
<input type="text" name="search"/>
<select class="form-control" name="field">
<option value="Name">Name</option>
<option value="EmailAddress">Email Address</option>
</select>
<button class="btn btn-primary" id="searchBtn">Search</button>
}
并且操作将是相同的(正如我在问题中所写的)。
这一切都是关于为输入类型添加一个名称 ,它应该与操作中的名称相匹配 并且它将被转发到操作。
非常感谢。
我正在尝试向我的项目中的搜索功能添加多个选项。 我知道简单的搜索就像:
<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);
}
}
但我想做的是添加指定搜索字段的功能,例如:电子邮件地址或按名称。 我想要实现的是添加一个 DropDownList 以添加选择要查看的字段的能力。操作类似于:
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);
}
但是如何在视图中实现这个场景呢? 如何通过视图中的表单将多个参数传递给控制器? 谢谢。
没关系,我想通了。 视图将像:
@using (Html.BeginForm("Search", "User"))
{
<input type="text" name="search"/>
<select class="form-control" name="field">
<option value="Name">Name</option>
<option value="EmailAddress">Email Address</option>
</select>
<button class="btn btn-primary" id="searchBtn">Search</button>
}
并且操作将是相同的(正如我在问题中所写的)。 这一切都是关于为输入类型添加一个名称 ,它应该与操作中的名称相匹配 并且它将被转发到操作。 非常感谢。