Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC $.post call returning string...need help with format for jqGrid
    text
    copied!<p>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:</p> <pre><code>var destinations = $.ajax({ type:"POST", url: '&lt;%= Url.Action("GetDestinations", "Logger") %&gt;', dataType: "json", async: false, success: function(data) { } }).responseText; </code></pre> <p>Now, the jqGrid wants the values for the dropdown formatted like this:</p> <pre><code>value: "FE:FedEx; IN:InTime; TN:TNT" </code></pre> <p>I'm using the StringBuilder to iterate through my collection and provide the proper string that the jqGrid wants:</p> <pre><code>foreach (var q in query) { sb.Append("ID:"); sb.Append(q.Destination); sb.Append("; "); } </code></pre> <p>I return this from my controller like this:</p> <pre><code>return this.Json(sb.ToString()); </code></pre> <p>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".</p> <p>I think the problem is when I debug in FireBug, the result for the jqGrid looks like this:</p> <pre><code>value: ""ID: One;ID: Two;ID: Three;ID: Four;ID: Five;"" </code></pre> <p>See how there are two sets of quotes. This is probably because when I say:</p> <pre><code>sb.ToString() </code></pre> <p>It probably generates the quotes and then the jqGrid adds a second set. But I'm not 100% on that.</p> <p>What is the best way to deal with this? Any advice would be greatly appreciated. </p> <p>SOLUTION:</p> <p>I solved this by using return ContentResult(sb.ToString();</p> <p>I would like to use the dataUrl method as Oleg mentioned but haven't got that working yet.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload