如何将表单数据和jqGrid(editUrl)数据同时传递给Controller [英] How to pass form data and jqGrid (editUrl) data to Controller at same time

查看:19
本文介绍了如何将表单数据和jqGrid(editUrl)数据同时传递给Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有各种表单数据位和 jqGrid 的 asp.net MVC3 应用程序.

I have an asp.net MVC3 app with various bits of form data and a jqGrid.

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

When I edit a row in the jqGrid I need to post the grid data as well as some of the form pieces to the editUrl controller.

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

I can post the jqGrid edited data to my controller through the editUrl just fine.

有没有办法做到这一点?

Is there a way to do this?

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

Im not sure how to send the other form elements and how to receive them in my controller.

任何帮助将不胜感激.

下面是我的 jqGrid:

Below is my 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 = '<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>";
                }
            }
        },
        { 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 属性.这些函数将在每次请求期间调用以获取网格的数据.以同样的方式,您可以使用 inlineDataextraparam您调用的 editRow 参数在您的 onSelectRow 回调中明确显示.

I see you use already programID and buildID properties defined as functions. The functions will be called during every request to get the data for the grid. In the same way you can use inlineData or extraparam parameter of the editRow which you call explicitly inside of your onSelectRow callback.

尝试调用 demo,它有以下 jqGrid 选项:

Try to call the demo which has the following jqGrid options:

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 流量(关于 FiddlerFirebug) 你会看到以下附加参数将被发送到 editurl:

You will see two alerts if you would save the data of the editing row. In my demo I used editurl: 'myDummyUrl' which has no code on the server side and you will see an error at the end, but if you examine the HTTP traffic (with respect of Fiddler or Firebug) you will see that the following additional parameters will be send to the editurl:

myParam=OK&myNextParam=Fine

我认为这是你需要的.

inlineData:{}

在编辑时进入控制器之前工作正常.但是在删除的情况下,如何像以前一样获取事件以将控制权传递给控制器​​以制作 json,然后单击删除.

is working fine for getting work before goes to controller while editing. But in case of delete how to get event like before to pass control to controller to make json , after click on delete.

这篇关于如何将表单数据和jqGrid(editUrl)数据同时传递给Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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