Home >Backend Development >C++ >Why Does My ASP.NET MVC $.post Request Return Unexpectedly Formatted Strings for jqGrid Dropdowns?
When using jqGrid to dynamically populate the drop-down menu for data editing, additional "undefined" items will appear in the drop-down menu. This issue stems from the string format used to populate the dropdown menu values.
jqGrid requires the drop-down menu value format to be as follows:
<code>value: "FE:FedEx; IN:InTime; TN:TNT"</code>
However, the code in the ASP.NET MVC action generates the string using sb.ToString(), which adds unnecessary quotes around the value:
<code>value: ""ID: One;ID: Two;ID: Three;ID: Four;ID: Five;""</code>
Method 1: Use dataUrl
In order to solve this problem, it is recommended to use the dataUrl attribute of editoptions or searchoptions in jqGrid. This allows you to specify a URL that returns results in the desired format:
<code>{ name: 'destinations', ditable: true, edittype:'select', editoptions: { dataUrl:'<%= Url.Action("GetDestinationList","Home") %>' } }</code>
In the controller, the GetDestinationList operation should return a JSON array containing the dropdown menu values:
<code>public JsonResult GetDestinationList() { List<string> allDestinations = GetAllDestinations(); Json(allDestinations, JsonRequestBehavior.AllowGet); }</code>
Method 2: Use the buildSelect function
If dataUrl is not available, you can use the buildSelect function to format the drop-down menu value:
<code class="language-javascript"> buildSelect: function(data) { var s = ''; if (response && response.length) { for (var i = 0, l=response.length; i<l ; i++) { var ri = response[i]; s += ''+ri+''; } } return s + ""; }</code>
This function receives the response data from the server and returns a string in the required format.
The above is the detailed content of Why Does My ASP.NET MVC $.post Request Return Unexpectedly Formatted Strings for jqGrid Dropdowns?. For more information, please follow other related articles on the PHP Chinese website!