Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you try to solve the problem for jqGrid only you can choose another way.</p> <p>You can use <a href="http://www.trirand.com/jqgridwiki/doku.php?id=wiki:common_rules#editoptions" rel="noreferrer">dataUrl</a> and <a href="http://www.trirand.com/jqgridwiki/doku.php?id=wiki:common_rules#editoptions" rel="noreferrer">buildSelect</a> properties of <a href="http://www.trirand.com/jqgridwiki/doku.php?id=wiki:common_rules#editoptions" rel="noreferrer">editoptions</a> or <a href="http://www.trirand.com/jqgridwiki/doku.php?id=wiki:search_config#colmodel_options" rel="noreferrer">searchoptions</a> instead of <code>value</code> property. This features are introduced specially for the usage in AJAX. The <a href="http://www.trirand.com/jqgridwiki/doku.php?id=wiki:common_rules#editoptions" rel="noreferrer">dataUrl</a> defines url provided results in the form like</p> <pre><code>&lt;select&gt;&lt;option value="1"&gt;One&lt;/option&gt; &lt;option value="2"&gt;Two&lt;/option&gt;&lt;/select&gt; </code></pre> <p>If for you is easer to return JSON results from the server your custom function <a href="http://www.trirand.com/jqgridwiki/doku.php?id=wiki:common_rules#editoptions" rel="noreferrer">buildSelect</a> will help. As the parameter it receive the data send from the server and it should return the string <code>&lt;select&gt;&lt;option&gt;...&lt;/option&gt;&lt;/select&gt;</code>. In the way you will achieve better results.</p> <p>If you do decide to stay at your old way you should at least fix your code to following</p> <pre><code>foreach (var q in query) { if (sb.Length != 0) sb.Append(';'); sb.Append(q.Destination); // instead of sb.Append("ID"); sb.Append(':'); sb.Append(q.Destination); } </code></pre> <p>to has <code>"FedEx:FedEx;InTime:InTime;TNT:TNT"</code> instead of <code>"ID:FedEx; ID:InTime; ID:TNT; "</code>.</p> <p><strong>UPDATED</strong>: You asked for a small example. Let us you can for example get all different values of the destinations strings as a <code>List&lt;string&gt;</code> and the name of this Method is <code>GetAllDestinations</code>. Then your action used by <a href="http://www.trirand.com/jqgridwiki/doku.php?id=wiki:common_rules#editoptions" rel="noreferrer">dataUrl</a> can look like </p> <pre><code>public JsonResult GetDestinationList() { List&lt;string&gt; allDestinations = GetAllDestinations(); Json(allDestinations, JsonRequestBehavior.AllowGet); } </code></pre> <p>To use this action inside of <a href="http://www.trirand.com/jqgridwiki/doku.php?id=wiki:common_rules#editoptions" rel="noreferrer">editoptions</a> or <a href="http://www.trirand.com/jqgridwiki/doku.php?id=wiki:search_config#colmodel_options" rel="noreferrer">searchoptions</a> of jqGrid you can define about following </p> <pre><code>{ name: 'destinations', ditable: true, edittype:'select', editoptions: { dataUrl:'&lt;%= Url.Action("GetDestinationList","Home") %&gt;', buildSelect: function(data) { var response = jQuery.parseJSON(data.responseText); var s = '&lt;select&gt;'; if (response &amp;&amp; response.length) { for (var i = 0, l=response.length; i&lt;l ; i++) { var ri = response[i]; s += '&lt;option value="'+ri+'"&gt;'+ri+'&lt;/option&gt;'; } } return s + "&lt;/select&gt;"; } } } </code></pre> <p>If you don't want have actions which be used per HTTP GET you can use <code>Json(allDestinations);</code> instead of <code>Json(allDestinations, JsonRequestBehavior.AllowGet);</code> in the <code>GetDestinationList</code> action, but add to the list of jqGrid options an additional option</p> <pre><code>ajaxSelectOptions: { type: "POST" } </code></pre> <p><strong>UPDATED 2</strong>: The answer is already old. In the interim the code of jqGrid where <code>buildSelect</code> will be called was changed. Now the <code>buildSelect</code> will be used inside of <code>success</code> handler of <code>jQuery.ajax</code> (see <a href="https://github.com/tonytomov/jqGrid/blob/v4.4.0/js/grid.common.js#L350-355" rel="noreferrer">here</a>) instead of the <code>complete</code> handler before (see <a href="http://www.trirand.com/blog/?page_id=393/bugs/dataurl-ajax-should-use-success-not-complete/#p17917" rel="noreferrer">the post</a> and <a href="http://www.trirand.com/blog/?page_id=393/bugs/all-usage-of-buildselect-should-be-inside-of-ajax-success/#p24174" rel="noreferrer">the post</a> for example). So in the current version of jqGrid the line</p> <pre><code>var response = jQuery.parseJSON(data.responseText); </code></pre> <p>is not needed. The <code>data</code> is typically the parsed JSON data and so the lines</p> <pre><code> buildSelect: function(data) { var response = jQuery.parseJSON(data.responseText); </code></pre> <p>in the code above can be replaced to</p> <pre><code> buildSelect: function(response) { </code></pre>
 

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