如何同时将表单数据和jqGrid(editUrl)数据传递给Controller

我有一个asp.net MVC3应用程序与各种forms的数据和一个jqGrid。

当我在jqGrid中编辑一行时,我需要将网格数据以及一些表单块发布到editUrl控制器。

我可以通过editUrl将jqGrid编辑的数据发布到我的控制器就好了。

有没有办法做到这一点?

我不知道如何发送其他表单元素以及如何在我的控制器中接收它们。

任何帮助将不胜感激。

下面是我的jqGrid:

$("#jqTable").jqGrid({ // Ajax related configurations url: '@Url.Action("_CustomBinding")', datatype: "json", mtype: "POST", postData: { programID: function () { return $("#ProgramID option:selected").val(); }, buildID: function () { return $('#Builds option:selected').val(); } }, // Specify the column names colNames: ["Actions", "Assembly ID", "Assembly Name", "Assembly Type", "Cost", "Order", "Budget Report", "Partner Request", "Display"], // Configure the columns colModel: [ { name: 'myac', width: 80, fixed: true, sortable: false, resize: false, formatter: 'actions', formatoptions: { keys: true} }, { name: "AssemblyID", key: true, index: "AssemblyID", width: 40, align: "left", editable: false }, { name: "AssemblyName", index: "AssemblyName", width: 100, align: "left", editable: true, edittype: 'select', editoptions: { dataUrl: '@Url.Action("_Assemblies")', buildSelect: function (data) { var response = jQuery.parseJSON(data); var s = ''; if (response && response.length) { for (var i = 0, l = response.length; i < l; i++) { var ri = response[i]; s += '' + ri + ''; } } return s + ""; } } }, { name: "AssemblyTypeName", index: "AssemblyTypeName", width: 100, align: "left", editable: false, edittype: 'select' }, { name: "AssemblyCost", index: "AssemblyCost", width: 50, align: "left", formatter: "currency", editable: true }, { name: "AssemblyOrder", index: "AssemblyOrder", width: 50, align: "left", editable: true }, { name: "AddToBudgetReport", index: "AddToBudgetReport", width: 100, align: "center", formatter: "checkbox", editable: true, edittype: 'checkbox' }, { name: "AddToPartnerRequest", index: "AddToPartnerRequest", width: 100, align: "center", formatter: "checkbox", editable: true, edittype: 'checkbox' }, { name: "Show", index: "Show", width: 50, align: "center", formatter: "checkbox", editable: true, edittype: 'checkbox'}], // Grid total width and height and formatting //width: 650, //height: 220, altrows: true, // Paging //toppager: true, pager: $("#jqTablePager"), rowNum: 10, rowList: [10, 20, 30], viewrecords: true, // Specify if "total number of records" is displayed emptyrecords: 'No records to display', // Default sorting sortname: "AssemblyID", sortorder: "asc", // Grid caption caption: "Build Template", // grid command functionality editurl: '@Url.Action("_AjaxUpdate")', onSelectRow: function (AssemblyID) { if (AssemblyID && AssemblyID !== lastsel) { $('#jqTable').jqGrid('restoreRow', lastsel); $("#jqTable").jqGrid('editRow', AssemblyID, true); lastsel = AssemblyID; } } }).navGrid("#jqTablePager", { refresh: false, add: false, edit: false, del: false }, {}, // settings for edit {}, // settings for add {}, // settings for delete {sopt: ["cn"]} // Search options. Some options can be set on column level ); 

我看到你使用已定义为函数的programIDbuildID属性。 在每次获取网格数据的请求期间都会调用这些函数。 以同样的方式,您可以使用在onSelectRow回调中显式调用的editRow的 inlineData或extraparam参数。

尝试调用具有以下jqGrid选项的演示 :

 inlineData: { myParam: function () { alert("inlineData is calling!!!"); return "OK"; } }, onSelectRow: function (id) { if (id && id !== lastSel) { $(this).jqGrid('restoreRow', lastSel); $(this).jqGrid('editRow', id, true, null, null, null, { myNextParam: function () { alert("extraparam of 'editRow' is calling!!!"); return "Fine"; } }); lastSel = id; } } 

如果要保存编辑行的数据,您将看到两个警报。 在我的演示中,我使用了editurl: 'myDummyUrl' ,它在服务器端没有代码,你最后会看到一个错误,但如果你检查HTTP流量(关于Fiddler或Firebug ),你会看到以下附加内容参数将发送到editurl

 myParam=OK&myNextParam=Fine 

我认为这是你需要的。

 inlineData:{} 

在编辑之前进入控制器之前,工作正常。 但是在删除的情况下如何获取事件就像之前将控件传递给控制器​​来制作json,点击删除后。

您可以使用onclickSubmit选项自定义发送到服务器的onclickSubmit

 .navGrid("#jqTablePager", { refresh: false, add: false, edit: false, del: false }, { // settings for edit onclickSubmit: function(params, postdata) { postdata.extraParam = 'value' } }, {}, // settings for add {}, // settings for delete {sopt: ["cn"]} // Search options. Some options can be set on column level ); 

控制器将接收包含所有已编辑属性+ extraParam的JSON对象。 您可以在服务器端处理此问题。