Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>the autocomplete control will post a json object to "servicePath/serviceMethod" so first, set the <code>servicePath</code> to your controller and the <code>serviceMethod</code> option to your action name. </p> <p>then define this class: </p> <pre><code>public class AutoCompleteRequest { public string PrefixText { get; set; } public int Count { get; set; } } </code></pre> <p>because thats the json object the autocomplete control posts to your controller. </p> <p>then define a JsonModelBinder: </p> <pre><code>public class JsonBinderAttribute : CustomModelBinderAttribute { public override IModelBinder GetBinder() { return new JsonModelBinder(); } public class JsonModelBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { try { var inputStream = controllerContext.HttpContext.Request.InputStream; using (var sr = new StreamReader(inputStream)) { var json = sr.ReadToEnd(); return JsonConvert.DeserializeObject(json, bindingContext.ModelType); } } catch { return null; } } } } </code></pre> <p>this one is using Json.Net as deserializer. </p> <p>then define your action like this: </p> <pre><code> public virtual JsonResult SearchA([JsonBinder]AutoCompleteRequest post) { var data = repository.Query&lt;A&gt;() .Where(s =&gt; s.Name.StartsWith(post.PrefixText)) .Take(post.Count) .Select(s =&gt; s.Name) .ToArray(); return Json(data); } </code></pre> <p>note how i use the JsonBinderAttribute on the AutoCompleteRequest parameter and how a Json array of strings is returned. </p> <p><em>Edit</em></p> <p>i did a follow up blog post here: <a href="http://devcarl.posterous.com/how-to-use-ajax-library-4-autocomplete-with-a" rel="nofollow noreferrer">http://devcarl.posterous.com/how-to-use-ajax-library-4-autocomplete-with-a</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload