asp.net mvc - jQueryUI + ASP .NET MVC autocomplete with json data -


i have big problem calling json jqueryui autocomplete. have simple js:

$(document).ready(function() {     $('#editor_tags').autocomplete({         source: "/forums/ajax/gettags",         focus: function () {             // prevent value inserted on focus             return false;         },         select: function (event, ui) {             var terms = split(this.value);             // remove current input             terms.pop();             // add selected item             terms.push(ui.tagname);             // add placeholder comma-and-space @ end             terms.push("");             this.value = terms.join(", ");             return false;         }     }); }); 

and model i'm trying return:

public class tagview {     public int tagid { get; set; }     public string tagname { get; set; }     public int weight { get; set; } } 

but that's not main issue. main issue is, when start typing, jquery not make request controller. i'm 100% sure, url speciefied good. because can manually access controller typing /forums/ajax/gettags?term=text , results it. i'm using newset version of jquery , jqui directly rom google cdn.

the jqueryui autocomplete widget expects data in source parameter meet following requirements:

[..] simple array of strings, or contains objects each item in array, either label or value property or both.

so have 2 options:

  1. change viewmodel you're serializing json meet requirements:

    public class tagview {     public string label { get; set; }     public string value { get; set; } } 
  2. change autocomplete widget's source parameter function in perform ajax call , format data appropriately:

    $("#editor_tags").autocomplete({     source: function (request, response) {         $.getjson("/forums/ajax/gettags", { term: request.term }, function (data) {             response($.map(data, function (el) {                 return {                     label: el.tagname,                     value: el.tagid                 };             }));         });     },     /* other autocomplete options */ }); 

    this assuming data returned server json array of tagview objects.

the second piece of code untested, should @ least in right direction.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -