ASP.NET MVC and jqGrid drop-down list data formatting: solving $.post calling problem
In ASP.NET MVC you can dynamically populate jqGrid's edit data dropdown list. However, you may encounter issues with "undefined" values appearing in dropdown lists. This may be caused by jqGrid's data formatting.
Problem Overview
When making a $.post call using jQuery, the data received from the controller may contain double quotes. This causes problems with jqGrid because it expects the value of the dropdown to be in the format "value:value;...".
Solution: Use ContentResult
To solve this problem, use ContentResult method instead of Json. The syntax of ContentResult is as follows:
<code class="language-csharp">return Content(sb.ToString());</code>
Alternative: use dataUrl and buildSelect
Another option is to use dataUrl and buildSelect attributes in jqGrid's editoptions or searchoptions. Using dataUrl, you provide a URL that returns data in the format required by jqGrid's select element. buildSelect is a custom function that receives data and returns the HTML of the select element.
Code Example
Here is an example of how to use dataUrl and buildSelect:
Controller operations:
<code class="language-csharp">public JsonResult GetDestinationList() { List<string> allDestinations = GetAllDestinations(); return Json(allDestinations, JsonRequestBehavior.AllowGet); }</code>
jqGrid options:
<code class="language-javascript">{ name: 'destinations', editable: true, edittype:'select', editoptions: { dataUrl: '/YourController/GetDestinationList', buildSelect: function(data){ var s = '<select>'; if (data && data.length) { for (var i = 0, l=data.length; i<l ; i++) { var ri = data[i]; s += '<option value="' + ri + '">' + ri + '</option>'; } } s += '</select>'; return s; }} }</code>
Summary
You can provide the correct data format for jqGrid dropdown values from $.post calls in ASP.NET MVC by using the ContentResult method or the dataUrl and buildSelect properties.
The above is the detailed content of How to Properly Format jqGrid Dropdown Responses from ASP.NET MVC $.post Calls?. For more information, please follow other related articles on the PHP Chinese website!