I apologize this is sort of a repeat, however even after looking at several other questions on the topic, I'm still not able to get mine working.
I want to use a JQuery AutoComplete widget in my form to select from 30K+ records in a database. I believe my issues stems around the source
of the .autocomplete
function.
HTML + JS
@using (Html.BeginForm())
{
@Html.LabelFor(model => model.Name)
@Html.TextBoxFor(model =>model.Name, new { id="Search"})
}
<script type="text/javascript">
$('#Search').autocomplete({
source: '@Url.Action("GetVendors", "TestAutoComplete")',
minLength: 3
});
</script>
Controller Action
[HttpGet]
public JsonResult GetVendors(string Search)
{
return Json(DB.VendorLookUp(Search), JsonRequestBehavior.AllowGet);
}
I know my DB.VendorLookUp method works (I've tested it separately) and I have a breakpoint inside the GetVendors method which doesn't get hit.
In the console I get this error:
GET http://localhost:50848/TestAutoComplete/GetVendors?term=mic 500 (Internal Server Error)
(where 'mic' is the search I attempted). I assume at least part of the problem is that my controller method is excepting string "Search", when the JS is trying to GET "term".
Thanks in advance for your assistance.
EDIT: Things I've already tried:
- Changing parameter "Search" to "term" in my controller action
- Not having any parameters in my controller action
- Updating the @Url.Action() to pass along the value to:
source: '@Url.Action("GetVendors", "TestAutoComplete", new {Search= $('#VendorSearch').val() })'
- but get the error
Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
UPDATE:
Thanks to comment suggestion, looking at the error via Network -> Inspect Response gave me a meaningful error. I was trying to catch the js call anyway I could so had 2 controller actions - one with a parameter, and one with no parameters. This was confusing the code. I removed the action with no parameters and now the controller action is behaving properly and DB.VendorLookUp is returning a List of Vendor_VMs (which the controller properly converts to a JSON object).
However, the results are not returning correctly - see image 3 below. The options are not in a dropdown and are not displaying any values.
- How do I update the cshtml / js to pass the Name along to the input form?
- How do I make the values appear in the appropriate dropdown manner?