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:
change viewmodel you're serializing json meet requirements:
public class tagview { public string label { get; set; } public string value { get; set; } }
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
Post a Comment