0

我正在使用带有 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 不是。

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

4

1 回答 1

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

这解决了我的问题。

于 2012-09-20T17:35:04.183 回答