如果仅尝试解决jqGrid的问题,则可以选择另一种方法。
您可以使用dataUrl和buildSelect的性质editoptions或searchoptions,而不是
value财产。专门针对AJAX中的使用引入了此功能。所述dataUrl定义URL的形式等提供的结果
<select><option value="1">One</option> <option value="2">Two</option></select>
如果您更轻松地从服务器返回JSON结果,则自定义函数buildSelect会有所帮助。作为参数,它接收从服务器发送的数据,并且应该返回字符串
<select><option>...</option></select>。这样您将获得更好的结果。
如果您决定保留原来的方式,则至少应将代码固定为遵循
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);}拥有
"FedEx:FedEx;InTime:InTime;TNT:TNT"而不是
"ID:FedEx; ID:InTime; ID:TNT; "。
更新
:您要求一个小例子。例如,让我们可以获取目的地字符串的所有不同值,
List<string>并将其命名为
GetAllDestinations。然后,dataUrl使用的操作看起来像
public JsonResult GetDestinationList() { List<string> allDestinations = GetAllDestinations(); Json(allDestinations, JsonRequestBehavior.AllowGet);}要在jqGrid的editoptions或searchoptions中使用此操作,您可以定义以下内容
{ name: 'destinations', ditable: true, edittype:'select', editoptions: { dataUrl:'<%= Url.Action("GetDestinationList","Home") %>', buildSelect: function(data) { var response = jQuery.parseJSON(data.responseText); var s = '<select>'; if (response && response.length) { for (var i = 0, l=response.length; i<l ; i++) { var ri = response[i]; s += '<option value="'+ri+'">'+ri+'</option>'; } } return s + "</select>"; } }}如果您不想让每个HTTP
GET使用动作,则可以在动作中使用,
Json(allDestinations);而不是使用,但可以在jqGrid选项列表中添加一个附加选项
Json(allDestinations,JsonRequestBehavior.AllowGet);``GetDestinationList
ajaxSelectOptions: { type: "POST" }更新2
:答案已经很旧了。在此期间,
buildSelect将调用jqGrid的代码已更改。现在,
buildSelect将在(参见此处)的
success处理程序内部而不是之前的处理程序中使用(例如,参见post和post)。因此,在当前版本的jqGrid中,
jQuery.ajax
complete
var response = jQuery.parseJSON(data.responseText);
不需要。的
data是典型地解析的JSON数据等的线
buildSelect: function(data) { var response = jQuery.parseJSON(data.responseText);上面的代码中可以替换为
buildSelect: function(response) {


