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

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

问题描述

我试图动态填充为当用户编辑数据的jqGrid一个下拉。我有美元。不过p $ ptty很多工作,没有在下拉列表中所说的不确定一个值。我怀疑这是因为这样我发送数据到网格。我使用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.

什么是对付它的最好方法?任何意见将大大AP preciated。

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

我想作为奥列格提到使用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 属性? ID =维基%3acommon_rules#editoptions> editoptions 或<一个href=\"http://www.trirand.com/jqgridwiki/doku.php?id=wiki%3asearch_config#colmodel_options\">searchoptions而不是属性。此功能是专门介绍了在AJAX的使用。该 dataUrl 定义URL提供结果的形式如

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>

如果你是easer从服务器返回JSON结果您的自定义功能 buildSelect 会有所帮助。作为参数它接收到的数据从服务器发送和它应该返回字符串&LT;选择&GT;&LT;选项&GT; ...&LT; /选项&GT;&LT; /选择&GT; 。在路上你会取得更好的效果。

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.

如果你决定留在旧的方式,你至少应该解决您的code到以下

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

到了联邦快递:联邦快递;银泰百货:银泰; TNT:TNT而不是ID:联邦快递; ID:银泰; ID: TNT。

更新时间::你问了一个小例子。让我们例如,您可以得到目标字符串作为一个名单,LT的所有不同的价值观;串&GT; 和本办法的名称是 GetAllDestinations 。然后,通过 dataUrl 用你的行动可以像

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 或<一内部这一行动href=\"http://www.trirand.com/jqgridwiki/doku.php?id=wiki%3asearch_config#colmodel_options\">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 :答案是已经老了。在jqGrid的其中 buildSelect 将被称为改为临时的code。现在 buildSelect 将内部使用成功的处理程序 jQuery.ajax (见这里 ),而不是之前(见<一个完整的处理程序href=\"http://www.trirand.com/blog/?page_id=393/bugs/dataurl-ajax-should-use-success-not-complete/#p17917\">the帖子和<一个href=\"http://www.trirand.com/blog/?page_id=393/bugs/all-usage-of-buildselect-should-be-inside-of-ajax-success/#p24174\">the帖子为例)。所以在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);

是没有必要的。在数据通常是解析的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);

在code以上可更换

                 buildSelect: function(response) {

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆