1

I've implemented a search box in an Asp.net MVC Project. The problem is that this function is case sensitive and I want it to be case-insensitive.

I need to change it somehow to disable case sensitivity.

Here is my jQuery code:

$('#FormSearch').submit(function (event) {
event.preventDefault();

    var formsearch = $('#FormSearch');
    var serializedForm = formsearch.serialize();
    var searchString = $('#search').val();
    var input = $('#search').val();
    if (searchString == '' || searchString == undefined || searchString == 0) {
        searchString = "";
    }
    else {
        searchString = 'search=' + searchString;
    }
    var url = '/Home/Help?' + searchString;
    var replacement = '<span class="highlight">' + searchString + '</span>'; // define replacement text

    $(".ajaxloader1").show();

    $.ajax({
        url: url,
        type: 'POST',
        cache: false,
        data: serializedForm,
        success: function (result) {       
            window.history.replaceState("#intagsname", "Title", url);
            $('#intagsname').html(result);
            var x = $('#hs li').length;
            for (var i = 1; i <= x; i++)
            {
                var text = $("#hs li:nth-child(" + i + ") h5").html();
                text = text.replace(input, '<span class="test">' + input + '</span>');
                $("#hs li:nth-child(" + i + ") h5").html(text);
                var textans = $("#hs li:nth-child(" + i + ") p").html();
                textans = textans.replace(input, '<span>' + input + '</span>');
                $("#hs li:nth-child(" + i + ") p").html(textans);
            }
        }
    });    
});

Here is my controller

    public ActionResult Help(string searchString)
    {
        searchString = Request["search"];
        var repsearch = new RepositorySearch();
        List<Question> question = new List<Question>();
        if (!string.IsNullOrEmpty(searchString))
        {
            question = repsearch.GetAllQuestion().Where(n => n.Qu.Contains(searchString)).ToList();
        }
        return Request.IsAjaxRequest() ? (ActionResult)PartialView("_QuestionPartial", question) : View(question);
    }

Here is my QuestionPartial

@using iranhoidaytour_com.Models
@if (Model.Count > 0)
{
 <ul id="hs">
    @foreach (Question q in Model)
    {
        <li class="">
            <h5 class="quos">@q.Qu</h5>
            <p class="answ">@q.Ans</p>
        </li>
    }
   </ul>
 }
RickL
  • 3,318
  • 10
  • 38
  • 39
neda Derakhshesh
  • 1,103
  • 2
  • 20
  • 43
  • 1
    Is it your search method in the controller you want to be none case sensitive?, if so try `repsearch.GetAllQuestion().Where(n => n.Qu.toLower().Contains(searchString.toLower())).ToList();` – Carsten Løvbo Andersen Apr 26 '17 at 10:21
  • Not related but just `url: '@Url.Action("Help", "Home"),` and `data: { searchString: $('#search').val(),` and delete all the code up until `var replacement = ...` –  Apr 26 '17 at 10:23
  • Refer [these answers](http://stackoverflow.com/questions/3360772/linq-contains-case-insensitive) –  Apr 26 '17 at 10:25
  • it would be more efficient to convert `searchString` to lowercase before passing it in the linq query. – King King Apr 26 '17 at 10:27
  • Convert parameter value and compare columns to lowercase will be enough – Zahid Mustafa Apr 26 '17 at 10:30
  • @CarstenLøvboAndersen thanks it works. appreciate if answer – neda Derakhshesh May 03 '17 at 14:37

0 Answers0