asp.net mvc - How do I pass my data to a JsonResult so that it formats correctly -
i using mootools textboxlist in mvc app create autocomplete tag suggester, similar stackoverflow one.
the script uses json suggestions. json string seems expect different able generate. script's demo, should this:
[[32,"science",null,null]]
but can't figure out how string come out of mvc quite that. best looks more like:
[{"id":11,"text":"science"}]
with actual field names showing up.
here controller method:
public jsonresult suggest(string search) { jsonresult jsonresult = new jsonresult(); var tags = t in db.tags t.text.contains(search) select new {id=t.tagid, text=t.text}; var result = dosomethingto(tags); // <---???????? jsonresult.data = result; jsonresult.jsonrequestbehavior = jsonrequestbehavior.allowget; return jsonresult; }
i've tried several variations of passing variables jsonresult.data without luck. i've tried arrays, custom objects, etc. i'm not getting it. i'm it's
edit: should have said "i'm it's easy."
it's array of arrays of objects. generate this:
return json(new[] { new object[] { 32, "science", null, null } });
and within select action try along lines of:
public actionresult suggest(string search) { var tags = t in db.tags t.text.contains(search) select new object[] { t.tagid, t.text }; return json(tags.tolist(), jsonrequestbehavior.allowget); }
Comments
Post a Comment