You can create a Rest API or a finished project using ASP.NET MVC and return the data as JSON. Controller function example:
public JsonpResult GetUsers(string userIds) { var values = JsonConvert.DeserializeObject<List<int>>(userIds); var users = _userRepository.GetAllUsersByIds(userIds); var collection = users.Select(user => new { id = user.Id, fullname = user.FirstName +" "+ user.LastName }); var result = new { users = collection }; return this.Jsonp(result); } public IQueryable<User> GetAllUsersByIds(List<int> ids) { return _db.Users.Where(c=> ids.Contains(c.Id)); }
Then you just call the GetUsers function through a regular AJAX function that supplies the array of identifiers (in this case I use jQuery stringify to send the array as a string and dematerialize it back to the controller, but you can just send the ints array and take it as an int array in the controller ) I created the entire Restful API using ASP.NET MVC, which returns data as a json cross domain and can be used from any application. This is, of course, if you can use ASP.NET MVC.
function GetUsers() { var link = '<%= ResolveUrl("~")%>users?callback=?'; var userIds = []; $('#multiselect :selected').each(function (i, selected) { userIds[i] = $(selected).val(); }); $.ajax({ url: link, traditional: true, data: { 'userIds': JSON.stringify(userIds) }, dataType: "jsonp", jsonpCallback: "refreshUsers" }); }
Vasile Laur Dec 27 '10 at 20:43 2010-12-27 20:43
source share