为什么我的jqGrid不能传递额外的表单数据?

时间:2012-09-20 17:19:05

标签: post jqgrid controller asp.net-mvc-4

我正在使用带有jqGrid的ASP.Net MVC 4,并且当jqGrid添加/编辑记录时,我正试图将表单中的附加值传递给Add和Edit控制器方法。我的jqGrid看起来像这样:

$('#jqgRulesSetupVariablesList').jqGrid({
    data: JSON.stringify(("#GroupDDL").valueOf()),
    contentType: 'application/json; charset=utf-8',
    //url from wich data should be requested
    url: '@Url.Action("GetRulesSetupVariables")',
    ondblClickRow: function (rowid) {
    jQuery(this).jqGrid('editGridRow', rowid,
        { url: '@Url.Action("UpdateRulesSetupVariable")', recreateForm: true, closeAfterEdit: true, closeOnEscape: true, reloadAfterSubmit: false });
    },
    //type of data
    datatype: 'json',
    //url access method type
    mtype: 'POST',
    //columns names
    colNames: ['ID', 'Name', 'Description', 'Type'],
    //columns model
    colModel: [
        { name: 'ID', index: 'ID', align: 'left', width: '30px', editable: false, key: true },
        { name: 'Name', index: 'Name', align: 'left', width: '100px', editable: true, edittype: 'text', editoptions: { maxlength: 20 }, editrules: { required: true } },
        { name: 'Description', index: 'Description', align: 'left', width: '260px', editable: true, edittype: 'text', editoptions: { maxlength: 80 }, editrules: { required: true } },
        { name: 'Type', index: 'Type', align: 'left', width: '25px', editable: true, edittype: 'text', editoptions: { maxlength: 1 }, editrules: { required: true } },
    ],
    //pager for grid
    pager: $('#jqgRulesSetupVariablesPaging'),
    //number of rows per page
    rowNum: 10,
    //initial sorting column
    sortname: 'Name',
    //initial sorting direction
    sortorder: 'asc',
    //we want to display total records count
    viewrecords: true,
    //grid height
    height: 230,
    width: 450,
});

$('#jqgRulesSetupVariablesList').jqGrid('navGrid', '#jqgRulesSetupVariablesPaging',
    { add: true, del: false, edit: true, search: false },
    // Edit Options
    { url: '@Url.Action("UpdateRulesSetupVariable")', closeAfterEdit: true, closeOnEscape: true, editData: { GroupName: function () { return "ABC"; } } },
    // Add Options
    { url: '@Url.Action("InsertRulesSetupVariable")', closeAfterAdd: true, closeOnEscape: true, editData: { GroupName: function () { return "ABC"; } } },
    // Delete options
    {}
);

我的控制器是:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UpdateRulesSetupVariable(RulesSetupVariableViewModel viewModel, string GroupName)
{
    bool updateSuccess = UpdateRulesSetupVariable_Internal(viewModel);

    return Json(updateSuccess);
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult InsertRulesSetupVariable(RulesSetupVariableViewModel viewModel, string GroupName)
{
    bool updateSuccess = UpdateRulesSetupVariable_Internal(viewModel);

    return Json(updateSuccess);
}

和我的模特:

public class RulesSetupVariableViewModel
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Type { get; set; }
}

ID,名称,描述和类型都很好,但GroupName不是。

我在这里做错了什么想法?

1 个答案:

答案 0 :(得分:0)

我明白了。愚蠢的错误。我正在使用doubleClick来编辑行。我修改了这一行

jQuery(this).jqGrid('editGridRow', rowid,
    { url: '@Url.Action("UpdateRulesSetupVariable")', 
      recreateForm: true, 
      closeAfterEdit: true, 
      closeOnEscape: true, 
      reloadAfterSubmit: false });
},

到此:

jQuery(this).jqGrid('editGridRow', rowid,
    { url: '@Url.Action("UpdateRulesSetupVariable")', 
      recreateForm: true, 
      closeAfterEdit: true, 
      closeOnEscape: true, 
      reloadAfterSubmit: false,
      editData: { GroupName: function () { return $("#GroupDDL").val(); } } 
},

这解决了我的问题。