筛选功能在ASP.NET MVC中不起作用

时间:2019-01-30 16:33:35

标签: c# entity-framework linq

在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>
}

1 个答案:

答案 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());

}

现在应该为您工作。