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


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


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:

<style>
.highlight {
background-color: #ff0;
color: #000;
}
</style>
@(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>
selectedItemsChanged(s, e) {
var arr = s.selectedItems,
html = '';
for (var i = 0; i < arr.length; i++) {
html += wijmo.format('<li>{country}</li>', arr[i]);
}
document.getElementById('selectedItems').innerHTML = html;
}
});
</script>
@(Html.C1().MultiAutoComplete()
.MaxItems(10).CssMatch("highlight")
.Bind(Model).OnClientSelectedItemsChanged("selectedItemsChanged")
)

<p>
<b>Selected Items:</b>
</p>
<ul id="selectedItems">
</ul>


  • onSelectedIndexChanged

    • Raises the selectedIndexChanged event





Try out MultiAutoComplete

Download the latest version of ComponentOne Studio