ASP.NET MVC $.post 调用返回字符串...需要 jqGrid 格式的帮助

ASP.NET MVC $.post call returning string...need help with format for jqGrid(ASP.NET MVC $.post 调用返回字符串...需要 jqGrid 格式的帮助)

本文介绍了ASP.NET MVC $.post 调用返回字符串...需要 jqGrid 格式的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在用户编辑数据时动态填充 jqGrid 的下拉列表.我已经很好地工作了,但是下拉调用未定义"中有一个值.我怀疑这是因为我将数据发送到网格的方式.我正在使用 ASP.NET MVC 2,我正在使用 jQuery 获取下拉列表的数据,如下所示:

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:

var destinations = $.ajax({ type:"POST",
                        url: '<%= Url.Action("GetDestinations", "Logger") %>',
                        dataType: "json",
                        async: false,
                        success: function(data) {

                         } }).responseText;

现在,jqGrid 希望下拉菜单的值如下所示:

Now, the jqGrid wants the values for the dropdown formatted like this:

value: "FE:FedEx; IN:InTime; TN:TNT"

我正在使用 StringBuilder 遍历我的集合并提供 jqGrid 想要的正确字符串:

I'm using the StringBuilder to iterate through my collection and provide the proper string that the jqGrid wants:

foreach (var q in query)
{
     sb.Append("ID:");
     sb.Append(q.Destination);
     sb.Append("; ");
}

我像这样从我的控制器返回这个:

I return this from my controller like this:

return this.Json(sb.ToString());

这一切都很好,我得到了下拉菜单所需的所有项目,但还有一个名为未定义"的额外项目(最后一个).

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".

我认为问题是当我在 FireBug 中调试时,jqGrid 的结果如下所示:

I think the problem is when I debug in FireBug, the result for the jqGrid looks like this:

value: ""ID: One;ID: Two;ID: Three;ID: Four;ID: Five;""

看看如何有两组引号.这可能是因为当我说:

See how there are two sets of quotes. This is probably because when I say:

sb.ToString()

它可能会生成引号,然后 jqGrid 添加第二组.但我不是 100% 的.

It probably generates the quotes and then the jqGrid adds a second set. But I'm not 100% on that.

解决这个问题的最佳方法是什么?任何建议将不胜感激.

What is the best way to deal with this? Any advice would be greatly appreciated.

解决方案:

我通过使用解决了这个问题返回 ContentResult(sb.ToString();

I solved this by using return ContentResult(sb.ToString();

我想使用 Oleg 提到的 dataUrl 方法,但还没有实现.

I would like to use the dataUrl method as Oleg mentioned but haven't got that working yet.

推荐答案

如果您尝试仅针对 jqGrid 解决问题,您可以选择其他方式.

If you try to solve the problem for jqGrid only you can choose another way.

您可以使用 dataUrl 和 buildSelect 属性trirand.com/jqgridwiki/doku.php?id=wiki:common_rules#editoptions" rel="noreferrer">editoptions 或 searchoptions 而不是 value 属性.此功能是专门为在 AJAX 中使用而引入的.dataUrl 定义 url 提供的结果,格式如下:/p>

You can use dataUrl and buildSelect properties of editoptions or searchoptions instead of value property. This features are introduced specially for the usage in AJAX. The dataUrl defines url provided results in the form like

<select><option value="1">One</option> <option value="2">Two</option></select>

如果您更容易从服务器返回 JSON 结果,您的自定义函数 buildSelect 会有所帮助.作为它接收从服务器发送的数据的参数,它应该返回字符串<select><option>...</option></select>.这样你会取得更好的结果.

If for you is easer to return JSON results from the server your custom function buildSelect will help. As the parameter it receive the data send from the server and it should return the string <select><option>...</option></select>. In the way you will achieve better results.

如果您决定继续使用旧方式,您至少应该将您的代码修复为以下内容

If you do decide to stay at your old way you should at least fix your code to following

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);
}

to 有 "FedEx:FedEx;InTime:InTime;TNT:TNT" 而不是 "ID:FedEx; ID:InTime; ID:TNT; ".

to has "FedEx:FedEx;InTime:InTime;TNT:TNT" instead of "ID:FedEx; ID:InTime; ID:TNT; ".

已更新:您要求举一个小例子.例如,您可以将目标字符串的所有不同值作为 List 获取,并且此方法的名称是 GetAllDestinations.那么 dataUrl 使用的操作可能看起来像p>

UPDATED: You asked for a small example. Let us you can for example get all different values of the destinations strings as a List<string> and the name of this Method is GetAllDestinations. Then your action used by dataUrl can look like

public JsonResult GetDestinationList() {
    List<string> allDestinations = GetAllDestinations();
    Json(allDestinations, JsonRequestBehavior.AllowGet);
}

要在 editoptions 中使用此操作或searchoptions 的 jqGrid 你可以定义如下

To use this action inside of editoptions or searchoptions of jqGrid you can define about following

{ 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); 而不是 Json(allDestinations, JsonRequestBehavior.AllowGet);GetDestinationList 操作中,但在 jqGrid 选项列表中添加一个附加选项

If you don't want have actions which be used per HTTP GET you can use Json(allDestinations); instead of Json(allDestinations, JsonRequestBehavior.AllowGet); in the GetDestinationList action, but add to the list of jqGrid options an additional option

ajaxSelectOptions: { type: "POST" }

更新 2:答案已经很老了.在此期间,将调用 buildSelect 的 jqGrid 代码已更改.现在 buildSelect 将在 jQuery.ajaxsuccess 处理程序中使用(参见 here) 而不是之前的 complete 处理程序(参见 帖子 和 帖子 例如).所以在当前版本的 jqGrid 中这条线

UPDATED 2: The answer is already old. In the interim the code of jqGrid where buildSelect will be called was changed. Now the buildSelect will be used inside of success handler of jQuery.ajax (see here) instead of the complete handler before (see the post and the post for example). So in the current version of jqGrid the line

var response = jQuery.parseJSON(data.responseText);

不需要.data 通常是解析后的 JSON 数据,因此行

is not needed. The data is typically the parsed JSON data and so the lines

                 buildSelect: function(data) {
                     var response = jQuery.parseJSON(data.responseText);

上面的代码中可以替换为

in the code above can be replaced to

                 buildSelect: function(response) {

这篇关于ASP.NET MVC $.post 调用返回字符串...需要 jqGrid 格式的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:ASP.NET MVC $.post 调用返回字符串...需要 jqGrid 格式的帮助

基础教程推荐