繁体   English   中英

如何修复MVC中的搜索功能

[英]How to fix search functionality in mvc

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

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

在我实现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);
            }

        }

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

您已设置:

        searchString = "";

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

首先删除

searchString = "";

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

 public ActionResult Index(string sortOrder, string searchString = "", string currentFilter, int? page, string searchBy, string startdate = null, string enddate = null)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM