Autocomplete for textbox in mvc

前端 未结 1 856
天涯浪人
天涯浪人 2021-01-29 05:05

This is my view and controller. I have converted code from c# to vb the code was working perfectly in C# but i dont know why this java script is not working in vb. I started deb

相关标签:
1条回答
  • 2021-01-29 05:42

    jQuery UI has an AutoComplete widget. The autocomplete widget is quite nice and straight forward to use. In this post, how to integrate the AutoComplete widget with an ASP.NET MVC application.

    The first step is to add the jQuery scripts and styles. With ASP.NET MVC 4, the following code does the work:

    @Styles.Render("~/Content/themes/base/css")
    @Scripts.Render("~/bundles/jquery")    
    @Scripts.Render("~/bundles/jqueryui")
    

    Using the AutoComplete widget is also simple. You will have to add a textbox and attach the AutoComplete widget to the textbox. The only parameter that is required for the widget to function is source. For this example, we will get the data for the AutoComplete functionality from a MVC action method.

    $(document).ready(function () {
    $('#tags').autocomplete(
        {
            source: '@Url.Action("TagSearch", "Home")'
    });
    })
    

    In the above code, the textbox with id=tags is attached with the AutoComplete widget. The source points to the URL of TagSearch action in the HomeController: /Home/TagSearch. The HTML of the textbox is below:

    <input type="text" id="tags" />
    

    When the user types some text in the textbox, the action method - TagSearch is called with a parameter in the request body. The parameter name is term. So, your action method should have the following signature:

    public ActionResult TagSearch(string term)
         {
    // Get Tags from database
    string[] tags = { "ASP.NET", "WebForms", 
                    "MVC", "jQuery", "ActionResult", 
                    "MangoDB", "Java", "Windows" };
     return this.Json(tags.Where(t => t.StartsWith(term)), 
                    JsonRequestBehavior.AllowGet);
    }
    
    0 讨论(0)
提交回复
热议问题