kgosi.itachi
kgosi.itachi

Reputation: 5

How to fix search functionality in mvc

My search function is not working. when I run the application it will display records fine

after enter an input in search box it displays all records no filtering

This used to work before I Implemented my viewModel

My View

@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>

My search inside my controller

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);
            }

        }

I expect the search filter records but it just does nothing

Upvotes: 0

Views: 260

Answers (2)

Rennnn
Rennnn

Reputation: 85

First remove

searchString = "";

As this line is resetting whatever value you pass into the controller to be empty. Then change the signature of your controller to

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

which will assign searchString a default value of "" if it's null.

Upvotes: 0

StriplingWarrior
StriplingWarrior

Reputation: 156459

You've set:

        searchString = "";

So your employees are either going to get filtered down to those whose JobTitle is "", or those whose FirstName or LastName start with "" (every string starts with "").

Upvotes: 1

Related Questions