Skip to main content Skip to footer

Getting Started with MultiAutoComplete for ASP.NET MVC

As of July 2017, users can select multiple items from a collection of objects in ASP.NET MVC with MultiAutoComplete. In the autocomplete control, selected items appear as tags in the text area of the control, and you can remove items from selection by clicking on the "X" in each tag. MultiAutoComplete MultiAutoComplete

Data Binding

You can bind the control to a collection of strings or objects by setting the Bind property to the source collection:


@(Html.C1().MultiAutoComplete().Bind(Model).CssMatch("highlight")  
    .SelectedIndexes(1,3)  

If you want to set some selections by default, use the SelectedIndexes property (see code above) to set the elements' indexes.

Binding Data to Complex Objects

MultiAutoComplete supports binding data to a collection of objects. Bind to the model as usual and set the DisplayMemberPath property to the object's property that's supposed to be displayed.


@(Html.C1().MultiAutoComplete()  
        .Bind(Model)  
        .DisplayMemberPath("Name")  
         .MaxSelectedItems(4))  

Set the MaxSelectedItems property to limit the number of items that can be selected at one time.

Incremental search — or real-time search — is a progressive search technique that MultiAutoComplete supports out-of-the-box. For example, if you need to display all types in an assembly — which is itself a huge list — it's best to use incremental search. Use ItemsSourceAction to set the controller action that filters on the server. For this example, use the following code to set up the controller action:


 public ActionResult TypesInMscorlib(string query, int max)  
        {  
            var types = typeof(object).Assembly.GetTypes();  
            return this.C1Json(types  
                .Where(t => t.FullName.ToUpper().Contains(query.ToUpper()))  
                .Select(t => t.FullName)  
                .Take(max).ToList(),  
                behavior: JsonRequestBehavior.AllowGet);  
        }  

In the view, set the ItemsSourceAction of the MultiAutoComplete control with the following code:


<br/>&nbsp;&nbsp;&nbsp;&nbsp;.highlight&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;background-color:&nbsp;#ff0;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;color:&nbsp;#000;<br/>&nbsp;&nbsp;&nbsp;&nbsp;}<br/>  
 @(Html.C1().MultiAutoComplete()  
        .MaxItems(10).CssMatch("highlight")  
        .ItemsSourceAction(Url.Action("TypesInMscorlib"))  
    )  

Use the CSSMatch property to set the CSS class that gets applied to the matching search strings.

Important Client APIs

While working with MultiAutoComplete, you often need to work with the client APIs of the control. You can use the client API similarly to ASP.NET MVC's other controls (read about it here). The full client API is included in the documentation. The control's most important properties and events/methods are:

  • the SelectedItems property
    • Gets the selected items of the controls
  • the SelectedItemChanged event
    • Occurs when the value of the selectedItems property changes. Here's an example:

<script><br/>selectedItemsChanged(s,&nbsp;e)&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp; var&nbsp;arr&nbsp;=&nbsp;s.selectedItems,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;html&nbsp;=&nbsp;'';<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(var&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;arr.length;&nbsp;i++)&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; html&nbsp;+=&nbsp;wijmo.format('<li>{country}</li>',&nbsp;arr[i]);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.getElementById('selectedItems').innerHTML&nbsp;=&nbsp;html;<br/> }<br/> });<br/></script>  
@(Html.C1().MultiAutoComplete()  
        .MaxItems(10).CssMatch("highlight")  
        .Bind(Model).OnClientSelectedItemsChanged("selectedItemsChanged")  
    )  



   **Selected Items:**  





  • onSelectedIndexChanged
    • Raises the selectedIndexChanged event

Try out MultiAutoComplete

Download the latest version of ComponentOne Studio

MESCIUS inc.

comments powered by Disqus