I'm trying to dynamically populate a dropdown for the jqGrid when the user is editing data. I have it pretty much working however, there is one value in the dropdown call "undefined". I suspect this is because of the way I'm sending the data to the grid. I'm using ASP.NET MVC 2 and I'm getting the data for the dropdown using jQuery like so:
var destinations = $.ajax({ type:"POST",
url: '<%= Url.Action("GetDestinations", "Logger") %>',
dataType: "json",
async: false,
success: function(data) {
} }).responseText;
Now, the jqGrid wants the values for the dropdown formatted like this:
value: "FE:FedEx; IN:InTime; TN:TNT"
I'm using the StringBuilder to iterate through my collection and provide the proper string that the jqGrid wants:
foreach (var q in query)
{
sb.Append("ID:");
sb.Append(q.Destination);
sb.Append("; ");
}
I return this from my controller like this:
return this.Json(sb.ToString());
This is all swell and I get all the items I need for the dropdown but there is an extra item (the last one) called "undefined".
I think the problem is when I debug in FireBug, the result for the jqGrid looks like this:
value: ""ID: One;ID: Two;ID: Three;ID: Four;ID: Five;""
See how there are two sets of quotes. This is probably because when I say:
sb.ToString()
It probably generates the quotes and then the jqGrid adds a second set. But I'm not 100% on that.
What is the best way to deal with this? Any advice would be greatly appreciated.
SOLUTION:
I solved this by using return ContentResult(sb.ToString();
I would like to use the dataUrl method as Oleg mentioned but haven't got that working yet.