-1

So the way I have got this setup involves ClientController in the Controllers Folder. And in the Views Folder, I have SearchClient.cshtml and _SearchClients.cshtml. I am using Ajax and following the tutorial from this link : http://techfunda.com/howto/291/search-database-using-ajax to get my search to work.

Here is how the methods in my controller look like :-

    public ActionResult SearchClient()
    {
        return View();
    }

    public PartialViewResult _SearchClients(string searchString = "")
    {
        var clients = repository.Get(c => c.isDeleted == false);
        clients = clients.Where(s =>
                                    (s.FirstName.Contains(searchString)) ||
                                    (s.MiddleName.Contains(searchString)) ||
                                    (s.LastName.Contains(searchString))
                                ).ToList();
        return PartialView(clients);
    }

And finally , here is the partial view :-

        @model IEnumerable<Entities.Client>
        @foreach (var item in Model)
        {
            <tr>
                <td>@item.FirstName</td>
                <td>@item.MiddleName</td>
                <td>@item.LastName</td>
                <td>@item.Phone</td>
                <td>@item.Email</td>
                <td>@item.AgencyID</td>
                <td>@item.StreetAddress</td>
                <td>@item.City</td>
                <td>@item.PostalCode</td>
                <td>@item.Province</td>
            </tr>
        }
Umar Aftab
  • 527
  • 4
  • 24
  • `500 (Internal Server Error)` means your server is throwing and exception. Use you browser tools (the Network tab) to inspect the response which will give the details of the error. –  Feb 12 '18 at 22:27
  • I went into the network tab and I was not aware that I could actually just click the link. When I did, it showed me the Yellow Error Screen with `Object Reference not set to an instance of an object ` And it appears on this line `(s.FirstName.Contains(searchString)) ||` – Umar Aftab Feb 12 '18 at 22:34
  • You need to debug your code! (put breakpoints in your controller method and the partial view and step through it) –  Feb 12 '18 at 22:37
  • @StephenMuecke Why would you want to give me a downvote, when I am trying to figure out the issue here, isnt this a relevant question ? – Umar Aftab Feb 12 '18 at 22:39
  • As it stands, the question is unanswerable and therefore not useful to anyone else. Edit it with the relevant information and I'll remove it (but if you just say _Object Reference not set to an instance of an object_ i'll just dupe it) –  Feb 12 '18 at 22:39
  • And if you debug your code, I'm certain that you will find that `searchString` is `null` because you did not enter a value in the textbox –  Feb 12 '18 at 22:42
  • @StephenMuecke When I do enter a value in the textbox, thats when the` Object Reference set to an instance of an object` error is thrown. Without using the search, the View returns with all the clients. – Umar Aftab Feb 12 '18 at 22:46
  • And I am not sure, what do you want me to change the question to, what should I ask, except what I see and comprehend ? – Umar Aftab Feb 12 '18 at 22:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165009/discussion-between-stephen-muecke-and-umar-aftab). –  Feb 12 '18 at 22:48
  • The bug was in the MiddleName, it was null for both the records. And I had not checked the string for being null or empty. Can you kindly remove the downvote and advice me on how to fix the question. – Umar Aftab Feb 13 '18 at 00:04
  • Please keep to chat –  Feb 13 '18 at 00:09
  • I have completed the Edit and I messaged you in the chat, but I did not hear back – Umar Aftab Feb 13 '18 at 20:58

1 Answers1

0

I figured that the MidddleName was null and had to remove it from the search. At the same time I had to check if the string was null or empty. Here is the Controller action.

    public PartialViewResult _SearchClients(string searchString = "")
    {
        if (!String.IsNullOrEmpty(searchString))
        { 
            var searchedClient = clients.Where(s =>
                                    (s.FirstName.Contains(searchString)) ||

                                    (s.LastName.Contains(searchString))
                                ).ToList();
            return PartialView(searchedClient);
        }
        return PartialView(clients);
    }

Hope this helps someone.

Umar Aftab
  • 527
  • 4
  • 24