如何修复MVC中的搜索功能

时间:2019-04-03 14:16:05

标签: c# .net asp.net-mvc entity-framework

我的搜索功能不起作用。当我运行应用程序时,它将显示记录很好

在搜索框中输入一个输入后,它将显示所有记录而不进行过滤

在我实现viewModel之前,它曾经可以工作

我的观点

@model WTCoro2.Models.PersonViewModel


@{
    ViewBag.Title = "People";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

@using (Html.BeginForm("Index", "People", FormMethod.Get))
{
    <b>Search By:</b>
    @Html.RadioButton("searchBy", "Name", true) <text>Name</text>
    @Html.RadioButton("searchBy", "Title") <text>Title</text>
   // @Html.RadioButton("searchBy", "Salary") <text>Salary</text>

    <br />
    @Html.TextBox("BusinessEntityID") <input type="submit" value="Search" />
}

<table class="table">
    <tr>
        <th>
            JobTitle
        </th>
        <th>
            @Html.DisplayNameFor(model => model.pers.First().FirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.pers.First().LastName)
        </th>
        <th>
            Email Address
        </th>
        <th>
           @Html.DisplayNameFor(model => model.phn.First().PhoneNumber)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.pers.First().BusinessEntity.BusinessEntityID)
        </th>
        <th></th>
    </tr>

    @if (Model.pers.Count() == 0)
    {
        <tr>
            <td colspan="7">No Record Found</td>
        </tr>
    }
    else
    {

        foreach (var item in Model.pers)
        {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Employee.JobTitle)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FirstName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.LastName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.EmailAddresses.First().EmailAddress1)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.PersonPhones.First().PhoneNumber)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.BusinessEntity.BusinessEntityID)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.BusinessEntityID }) |
                @Html.ActionLink("Details", "Details", new { id = item.BusinessEntityID }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.BusinessEntityID })
            </td>

        </tr>
        }
    }

</table>

我在控制器中进行搜索

public ActionResult Index(string sortOrder, string searchString, string currentFilter, int? page, string searchBy, string startdate = null, string enddate = null)
        {
            var mymodel = new PersonViewModel();
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            var people_list = mymodel.pers = db.People.ToList();
            var employee_list = mymodel.emp = db.Employees.ToList();
            var history_list = mymodel.history = db.EmployeeDepartmentHistories.ToList();
            var email_list = mymodel.emldrs = db.EmailAddresses.ToList();
            var phone_list = mymodel.phn = db.PersonPhones.ToList();
            /* if (searchString != null)
             {
                 page = 1;
             }
             else
             {
                 searchString = currentFilter;
             }
             ViewBag.CurrentFilter = searchString;*/
            searchString = "";
            if (searchBy == "Title")
            {
                mymodel.emp = (employee_list.Where(x => x.JobTitle == searchString || searchString == null).ToList());
                return View(mymodel);
            }
            /*  else if (startdate != null && enddate != null)
              {
                  DateTime start = DateManager.GetDate(startdate) ?? DateTime.Now;
                  DateTime end = DateManager.GetDate(enddate) ?? DateTime.Now;
                  return View(history_list.Where(x => x.StartDate >= start && x.EndDate <= end).ToList());

              }*/
            else
            {
                mymodel.pers = (people_list.Where(x => x.FirstName.StartsWith(searchString) || x.LastName.StartsWith(searchString) || searchString == null).ToList());
                return View(mymodel);
            }

        }

我希望搜索过滤器记录,但它什么也没做

2 个答案:

答案 0 :(得分:1)

您已设置:

        searchString = "";

因此,您的员工将被过滤到JobTitle为“”或FirstName或LastName以“”开头(每个字符串以“”开头)的员工。

答案 1 :(得分:0)

先删除

<Text>

此行正在重置您传递给控制器​​的任何值为空的值。然后将控制器的签名更改为

searchString = "";

,如果为null,它将为searchString分配默认值“”。