I have a difficult autocomplete problem. This is for the messaging system for the website I'm working on. I want it to work where you enter the username, it returns with the thumb of their image and their name and their identifier. Then, when you select it, I want it to display the username, but when it is sent back, I want it to send its identifier back (since the username is not unique).
I started with http://blog.schuager.com/2008/09/jquery-autocomplete-json-apsnet-mvc.html as an approach. However, I use tageditor.js from Stackoverflow as my extender, simply because I like the way it works.
The tag editor is linked below. I think this is an older version.
We are using MVC 1.0. Here is my code:
public ActionResult Recipients(string prefix, int limit) { IList<UserProfile> profiles = profileRepository.GetUsers(prefix, limit); var result = from p in profiles select new { p.ProfileId, p.FullName, ImageUrl = GetImageUrl(p) }; return Json(result); }
Here is the script on the page
<script type="text/javascript"> $(document).ready( function() { $('#recipients').autocomplete('<%=Url.Action("Recipients", "Filter") %>', { dataType: 'json', parse: function(data) { var rows = new Array(); for(var i=0; i < data.length; i++) { rows[i] = { data: data[i], value: data[i].ProfileId, result: data[i].FullName }; } return rows; }, formatItem: function(row, i, n) { return '<table><tr><td valign="top"><img src="' + row.ImageUrl + '" /></td><td valign="top">' + row.FullName + '</td></tr></table>'; }, max: 20, highlightItem: true, multiple: true, multipleSeparator: ";", matchContains: true, scroll: true, scrollHeight: 300 }); }); </script>
So what happens, the callback works, the image and username are displayed on my list, and when I select it, it puts the full username in the delimited text box. However, when I submit the form, only the names are sent back, not the profile identifiers. Any ideas on how to return the identifier without displaying them in the text box?
EDIT: Here is the version of tageditor.js I am using http://www.gotroleplay.com/scripts/tageditor.js