0

I am trying to create search box which show results when user is typing in.

I have the following in my partial view:

@Html.EditorFor(m => m.CategoryName, new { htmlAttributes = new { @id = "inputCat", @class = "form-control", @placeholder = "Enter category name...", @spellcheck = "true" } })

In my main view where I load the partial view I have:

$('#inputCat').autocomplete({
    source: 'Editor/SearchCategory'
});

Here is my controller action:

public ActionResult SearchCategory(string term)
{
    ApplicationDbContext db = new ApplicationDbContext();
    return Json(db.Categories.Where(cat => cat.CategoryName.StartsWith(term)), JsonRequestBehavior.AllowGet);
}

When I type in the input field in the browser console I get this error:

GET http://localhost:port/Editor/SearchCategory?term=s 500 (Internal Server Error)

I cannot understand where is the problem. Any help will be appreciated. Thanks in advance.

2 Answers 2

1

i would add comment but i cant yet.

please post more details because HTTP 500 can mean a lot of things

Have you looked at browser developer tools network tab for details of error ?

Have you tried to debug it and get exception details ?

maybe turn off customErrors in web.config to get YSOD with details about problem, and post them here.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your fast response. I have checked the Network tab /section Preview/ on my browser. And I thin maybe I understand what is happening. But I have a question: If my input field is in form that is posting data to some other controller action, can this autocomplete method from jQuery UI post the form?
i don't understand. please clarify. autocomplete does not "Submit" form it makes AJAX request
My input field, on which I use the jQuery UI autocmpolete method, is in form. I am posting the form to action method in one of my controllers. When I checked the Network tab and clicked the error another window with Preview tab showed. I clicked on that Preview tab and I got a detailed page with the error. I saw that my form was posted and because I have parameters that are passed as 'null', which I am processing afterwards, I thought that the error is related to that. So that is why I asked if autocomplete method can post my form "accidentally". Hope I was More clear now.
from my knowledge autocomplete should not post form. look what is in response of that 500 request, maybe there is information what happened and post it
Thanks for your help I will struggle with this a bit more :) Sorry I cannot vote this as answer =\
0

I got the solution. I needed to create new routing in the RouteConfig.cs file. Then I change the source to use '@Html.Action("ActionMethodName", "ControllerName")' This worked like a charm for me :) Hope this helps someone else who is in trouble.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.