I think I am missing something obvious while attempting to add autocomplete functionality in MVC 4. From what I have found in other posts I have been able to put together an example however the method in my controller is not being called.
What I have tried so far...
_Layout
@Styles.Render("~/Content/themes/base/css")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")
@Scripts.Render("~/bundles/jqueryval")
Controller
Public Function Numbers(term As String) As ActionResult
Return Json(New String() {"one", "two", "three", "four", "five", "six"}, JsonRequestBehavior.AllowGet)
End Function
View (I have hard coded the Home/Numbers for now)
<div class="editor-label">
@Html.LabelFor(Function(model) model.Number)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.Number)
@Html.ValidationMessageFor(Function(model) model.Number)
</div>
<script type="text/javascript">
$(function () {
$("#Number").autocomplete({
source: 'Home/Numbers',
minLength: 1
});
});
</script>
When I run my app and type in the textbox nothing happens. I have put a breakpoint in the "Numbers" function and it seems that it is never called.
My project can be found here http://www.filedropper.com/testapp
Razor is one of the view engines supported in ASP.NET MVC. Razor allows you to write a mix of HTML and server-side code using C# or Visual Basic.
The ASP.NET Web Forms AutoComplete is a textbox control that provides a list of suggestions to select from as the user types. It has several out-of-the-box features such as data binding, filtering, grouping, UI customization, accessibility, and more.
Razor view engine in asp.net mvc is the syntax that allows you to write server-side code on view. It helps to combine code and HTML in a fluid manner. Razor is not a new programming language. If you know C#, Vb.Net, and bit HTML, you can easily write Razor code.
If you have the @Scripts.Render
lines at the bottom of the body
element in the layout and after the @RenderBody()
you need to put your script in the scripts
section:
@section scripts
<script type="text/javascript">
$(function () {
$("#Number").autocomplete({
source: '@Url.Action("Numbers","Home")',
minLength: 1
});
});
</script>
End Section
Because your script depends on jQuery so jQuery should be loaded first.
Or just move your @Scripts.Render
declaration into the head
in the layout then you don't need to put your code into the scripts
section (but you are better off with using the section)
I suggest you to control errors in Chrome to ensure that jQuery libraries working properly. if there is no problem, Try this script :
$(document).ready(function () {
$("#Number").each(function () {
$(this).autocomplete({ source: $(this).attr("data-autocomplete") });
});
});
Then in your Razor (C#):
<input type="text" id="Number" data-autocomplete="@Url.Action("Action","Controller")" autocomplete="on" />
If you want to use Razor Html Helpers instead of using 'input' tag, The id attribute is the same name of Model.Member. Notice that in Controller, you must input string with the 'term' name, as written in your code. For security reasons, you must avoid using parameters in js file that shows the site technology. The method declared above never uses js file to get address of resource.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With