English 中文(简体)
向控制器提交jqGrid行数据 - 什么方法?
原标题:Submitting jqGrid row data from view to controller - what method?

我一直在努力将所有从jqGrid到从jqGrid到行动法的行的代码提交到行动方法,使用来自, < a href=' http://www.trirand.com/jqgridwiki/duku.php?id=wiki:methods" rel=“nofolnoreferr” >jqGrid Wiki 。在提交时,我希望所有行数据都回到控制器,这样我才能坚持它。我试过使用隐藏的字段存储所有行数据,但控制器似乎永远得不到所有东西,只有电网中的最后一个编辑的单元格。所以我切换成了一个jax方法,但是不管我怎么尝试什么我怎么才能把I 带到了Jsrox_col_col_col_cone.

$("#submitButton").click(function () {
    $("#awesomeGrid").jqGrid( resetSelection );
    var gridRows = $("#awesomeGrid").jqGrid( getRowData );
    var rowData = new Array();
    for (var i = 0; i < gridRows.length; i++) {
        var row = gridRows[i];
        rowData.push($.param(row));
    }
    var dataToSend = JSON.stringify(rowData);
    $.ajax({
        url:  @Url.Action("UpdateAwesomeGridData") ,
        type:  POST ,
        data: { gridData: dataToSend },
        dataType:  json ,
        success: function (result) {
            alert( success );
        }
    });
    return true;
});

和我的控制器方法签名:

[HttpPost]
public ActionResult UpdateAwesomeGridData(string gridData)

我尝试过将 gridData 参数从 string 改为 string[] ,到 object[] 到所有种类的东西,但似乎没有任何效果。如果我将它保留为 string ,我就会得到数据,但格式很古怪,像这样(栏名替换):

gridData=["Id=1&Field1=1945&Field2=0&Field3=0&Field4=1&Field5=Some+string+value&Field6=&Field7=&Field8=&Field9s=","Id=2&Field1=1945&Field2=0&Field3=0&Field4=2&Field5=Another+string+value&Field6=&Field7=&Field8=&Field9s=","Id=3&Field1=1945&Field2=0&Field3=0&Field4=3&Field5=Yet+another+string&Field6=&Field7=&Field8=&Field9s=","Id=4&Field1=1945&Field2=0&Field3=0&Field4=4&Field5=Final+string&Field6=&Field7=&Field8=&Field9s="]

我从小提琴手那里得到了这个, 并且记录下来, JSON 标签在我小提琴的时候没有显示任何请求 。 我可以用什么方法使这个阵列化, 并一次性发送它? 我的行动方法上应该使用哪种参数?

<强 > EDIT - 解决方案

对于像我一样蠢的其他人来说, 这就是我如何让它工作的:

首先,根据Oleg 的建议,我在 jqGrid 定义中添加了 < code> loadonce: true 。 然后,将我的提交按钮功能修改如下:

$("#submitButton").click(function () {
    var griddata = $("#awesomeGrid").jqGrid( getGridParam ,  data );
    var dataToSend = JSON.stringify(griddata);
    $.ajax({
        url:  @Url.Action("UpdateAwesomeGridData") ,
        type:  POST ,
        contentType:  application/json; charset=utf-8 ,
        data: dataToSend,
        dataType:  json ,
        success: function (result) {
            alert( success:   + result.result);
        }
    });
    return true;
});

然后更改了我的控制器方法签名 :

public ActionResult UpdateAwesomeGridData(IEnumerable<GridBoundViewModel> gridData)

希望这能帮助未来的人

最佳回答

我看见你有很多问题

第一个问题是,您使用 $.param(row) < param(row) 并用编码 (???????!!!) 数据填充数组 。 我认为正确的代码应该包含直接张贴从 getRowData 返回的数据: : data: {getData: greedRows {/code>。 您可以在服务器一侧使用 UpdateAweomeGridData(string gedData) , 然后将 gridData 转换为 List<List<string> > 例如,

JavaScriptSerializer serializer = new JavaScriptSerializer();
var myListOfData = serializer.Deserialize<List<List<string>>>(gridData);

我想退一步问一个问题:为什么你需要将数据发送到它已经拥有的服务器上?如果您需要从网格中获取的所有数据,您只需使用控制器的相同动作来生成数据,这样您就可以在服务器上拥有相同的数据 < 坚固 > < /坚固 > 。

将数据发送到服务器只在一种情况下才需要 : 您使用了 < code>loadonce: true , 修改了数据 local , 在修改结束时您需要将所有更改过的数据发送到服务器。 如果使用 getRowData , 则不是最佳选择, 因为它只能从当前页面 < / em> 获取数据 。 要获取所有数据, 您最好使用 < code> getGridParam ("数据")

问题回答

我发现Oleg已经回答了你的问题, 我无意覆盖它, 但我想在这里分享我的想法。 我认为通过数据作为MVC控制器的物体比字符串更有意义, 然后你就可以把这个东西传给你的 DAO 类。

这是我的jQuery代码(显然要感谢Oleg的这个代码)。

        var ruledetail = new Array();
        var grid = $( #RuleCriteria )[0], rows = grid.rows,
                    cRows = rows.length, iRow, iCol;


        var fromvalue;

        for (iRow = 0; iRow < cRows; iRow++) {
            row = rows[iRow]; // row.id is the rowid
            if ($(row).hasClass("jqgrow")) {
                cellsOfRow = row.cells;

                //build rule detail data
                ruledetail.push({
                    ColumnName: jQuery( #RuleCriteria ).jqGrid( getCell , row.id,  ColumnName ), 
                    ColumnOperator: jQuery( #RuleCriteria ).jqGrid( getCell , row.id,  ColumnOperator ),
                    FromValue: fromvalue,
                    ToValue: $(cellsOfRow[4]).text(),
                    Connector: jQuery( #RuleCriteria ).jqGrid( getCell , row.id,  Connector )
                });

            }
        }


        var ruledata = JSON.stringify({  detaildata : ruledetail });

        $.ajax({
            url: "@Url.Action("CreateRules", "Admin")",
            data: ruledata,
            type: "POST",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function () {
                $(".Message").displayMessage("@Msg.Success");
                setTimeout(function () { location.href = "@Url.Action("Rule", AppType)"; }, 1000);
                },
            error: function () {
                $(".Message").displayError("@Msg.GeneralError");
                }
        });

这是我的MVC密码

    [HttpPost]
    public ActionResult CreateRules(List<RuleData> detaildata)
    {

       RuleManager _savedata = new RuleManager();
       _savedata.Save(detaildata);
        var jsonData = "success";
        return Json(jsonData, JsonRequestBehavior.AllowGet);

    }

Jsp 页面的代码发送 :

  $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    url: "LoadDataservice.asmx/SaveData",
                    async: true,
                    data:  {"formData":"  + $( #check-user ).serialize() +  "} ,  // working
                    dataType: "json",
                    success: function (msg) {
                        $( #details ).empty();
                        jsonArray = $.parseJSON(msg.d);
                        var $ul = $( <ul id="details"> );
                        for (i = 0; i < jsonArray.length; i++) {
                            $("#details").append( <li id="  + i +  " name="head" >  + jsonArray[i].name +  </li> );
                        }
                        $( #details ).listview( refresh );
                    },
                    error: function (msg) {
                        alert("Error");
                    }
                });

行动类 :

 public string SaveData(string formData)
        {

            string s = formData;
            var keyValuePairs = from array in
                                    (from kvpString in s.Split( & )
                                     select kvpString.Split( = ))
                                select new KeyValuePair<string, string>(array[0].Trim(), array[1].Trim());
            var dictionary = keyValuePairs.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
            var username = dictionary["username"];
            var password = dictionary["password"];


            string details = "testing";

            return details;
        }
    }




相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...