I’m returning JSON from my controller to my view in order to populate a jquery autocomplete textbox in MVC. The problem is, some of my data contains commas, and therefore is split by the autocomplete helper.
Heres my code.
Controller:
public ActionResult GetData()
{
var data = repository.GetData();
return Json(data);
}
View (script):
$.post("../MyController/GetData",
function(data) {
var evalData = eval(data) + ""; //formats the text
$("#Data").autocomplete(evalData.split(","),
{
max: 500,
matchContains: true
});
});
As you can see, I am using the jquery .split helper to split the returned Json. Should I be using regex or should I go with a completely different approach?
I assume you’re using the Autocomplete built into jQuery UI 1.8. If you are, you have a couple of different options.
Based on one of the samples that’s available in documentation, you can just give it a string specifying the URL of your service and not have to worry about parsing the return yourself. So something along the lines of:
Your action will most likely need to respond to get requests as well as post though and your data may need to be in the form of
[{ label: "something", value: "1" }, ... ]Which you could do by shaping your data using a Linq query before sending it out.
You can combine your current implementation with parts of the example above to and get something like this:
This assumes that data is in the form of
[{ label: "something", value: "1" }, ... ](see 1 for how to shape it using Linq). The JSON parser will take care of commas in quotes problem for you.You can also specify a method to call when you want to retrieve data.
(see above about how to shape data using Linq)
A couple of comments about your current implementation.
You should considering using the UrlHelper instead of hard-codeing the URL in case you ever change your routes.
Instead of eval you should use the JSON2.js library to parse the return value from your action. It is generally a little bit more secure and yields better performance in newer browsers that support native JSON parsing.