0

controller:

[HttpPost]
    public JsonResult Index(string search)
    {
        var data = LoadExplore_SP("sp_View_Explore");
        List<ExploreModel> stuff = new List<ExploreModel>();
        foreach (var row in data)
        {
            stuff.Add(new ExploreModel
            {
                Thing_Title = row.Thing_Title
            });
        }
        //Searching records from list using LINQ query  
        var ThingList = (from N in stuff
                        where N.Thing_Title.Contains(search)
                        select new { N.Thing_Title });

        return Json(ThingList, JsonRequestBehavior.AllowGet);
    }

view:

<script type="text/javascript">
$(document).ready(function () {
    $("#Thing_Title").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/Home/Index",
                type: "POST",
                dataType: "json",
                data: { search: request.term },
                success: function (data) {
                    response($.map(data, function (item) {
                        return { label: item.Thing_Title, value: item.Thing_Title };
                    }))

                }
            })
        },
        messages: {
            noResults: "", results: ""
        }
    });
})

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

        <hr />

        <div class="form-group">

            <div class="col-md-12">
                @Html.EditorFor(model => model.Thing_Title, new { htmlAttributes = new { @class = "form-control" } })

            </div>
        </div>

    </div>
}

My problem is that I can't get the linq where clause to work the way it should. Instead of returning results like %search%, it is returning all records like 'search%' instead.

Do you know why it's behaving this way?

sion_corn
  • 3,043
  • 8
  • 39
  • 65
  • Try this N.Thing_Title.Trim().ToLowerInvariant().Contains(search.Trim().ToLowerInvariant()) – Prasanth Kumar Vinakota May 14 '19 at 04:09
  • no luck, it is still behaving like .StartsWith – sion_corn May 14 '19 at 04:11
  • Remove this line data: { Prefix: request.term }, and change url to this url: "/Home/Index?search=" + request.term and check once – Prasanth Kumar Vinakota May 14 '19 at 04:15
  • Try this too SqlMethods.Like(N.Thing_Title.Trim().ToLowerInvariant(), "%" + search.Trim().ToLowerInvariant() + "%") – Prasanth Kumar Vinakota May 14 '19 at 04:22
  • Are you sure `search` has valid value receiving from post call?? – Jaydip Jadhav May 14 '19 at 04:31
  • As for why the `Contains()` is actually using an Overload of `IndexOf()` as described by [mnemonic](https://stackoverflow.com/a/35371357/2417602) and if you want to have a `WildCard` search functionality take a look at the helper described by [Jon Koeter](https://stackoverflow.com/a/29298128/2417602). – vikscool May 14 '19 at 04:55
  • @PrasanthKumarVinakota none of these suggestions work – sion_corn May 14 '19 at 14:07
  • @vikscool I tried filtering using a variation of IndexOf() but it still behaves like StartsWith(). I don't care about wildcard functionality, so I will pass on the helper. – sion_corn May 14 '19 at 14:08
  • @sion_corn can you try the Helper method by [Jon koeter](https://stackoverflow.com/a/29298128/2417602). Also, you can take a look at [SqlMethods.Like()](https://learn.microsoft.com/en-us/dotnet/api/system.data.linq.sqlclient.sqlmethods.like?view=netframework-4.8). – vikscool May 14 '19 at 14:55
  • @sion_corn The `.Contains()` is supposed to work like `%search%`.Can you change your `where` condition to be something like `where N.Thing_Title.ToLower().Trim().Contains(search.ToLower().Trim())`. And if that does not work, will it be possible for you to provide some sample data like what is been passed from `client-side` and what source of data is its query against. – vikscool May 15 '19 at 05:07
  • @sion_corn And `JsonRequestBehavior.AllowGet` is unnecessary in your controller code as it is not a `Get` request(*as per the provided code*). But if you still want to use it you should change the `HTTPVerb` from `HttpPost` to `HttpGet` and change the `ajax` request as well. – vikscool May 15 '19 at 05:13

0 Answers0