在asp.net mvc中,我正在尝试过滤表,但是它不起作用
在“我的控制器方法”中:
public ActionResult Index(string sortOrder, string searchString)
{
var employeesSearch = from s in db.Employees
select s;
if (!String.IsNullOrEmpty(searchString))
{
employeesSearch = employeesSearch.Where(s => s.FullName.Contains(searchString)
|| s.Site.SiteName.Contains(searchString)
|| s.Area.Area1.Contains(searchString)
|| s.Discipline.Discipline1.Contains(searchString));
}
}
在我看来:
@using (Html.BeginForm())
{
<p>
Find by name: @Html.TextBox("SearchString")
<input type="submit" value="Search" />
</p>
}
答案 0 :(得分:1)
按如下所示在您的ActionName
中指定ControllerName
FormType
和@Html.BeginForm()
:
@using (Html.BeginForm("Index","YourControllerName", FormMethod.Post))
{
<p>
Find by name: @Html.TextBox("SearchString")
<input type="submit" value="Search" />
</p>
}
然后在Index
方法中:
public ActionResult Index(string sortOrder, string searchString)
{
var employeesSearch = from s in db.Employees
select s;
if (!String.IsNullOrEmpty(searchString))
{
employeesSearch = employeesSearch.Where(s => s.FullName.Contains(searchString)
|| s.Site.SiteName.Contains(searchString)
|| s.Area.Area1.Contains(searchString)
|| s.Discipline.Discipline1.Contains(searchString));
}
return View(employeesSearch.ToList());
}
现在应该为您工作。