我有一个现有的项目需要维护,但这是我第一次遇到jqGrid...
主要是,我有一个Product
,可以有多个Formule
。每个Formule
可以包含多个Period
。Period
由DepartureDate
和Price
描述。
这是管理句点网格的代码。特别是,它添加了网格的导航器,并有可能添加Period
s。
在网格中添加新行时,用户填写一个包含 2 个字段的表单:出发日期和与新创建的期间相对应的价格。
jQuery("#periode-grid").jqGrid(
'setGridParam',
{
postData: {
formuleId: rowid // <<< !!!
},
datatype: "json"
})
.navGrid("#periode-pager",
{
"edit": false, "add": true, "del": false,
"search": true, "addtext": "@Resources.Main.Grid_Add"
},
{},
{
"url": '@Url.Action("AddAjaxPeriod",
new { elementId = @ViewData["ProductId"] })', // <<< !!!
"closeOnEscape": true, "closeAfterAdd": true,
"reloadAfterSubmit": true, "width": 500,
"beforeSubmit": BeforeFormuleSubmit
});
这是我的AddAjaxPeriod
签名,包含 4 个参数,包括日期和价格:
[HttpPost]
[AjaxRequestOnly]
[Transaction]
public JsonResult AddAjaxPeriod(Guid elementId, Guid formuleId,
DateTime departureDate, double price)
{ ... }
现在一切正常,直到我打开表单添加价格和日期,填写请求的日期和价格,然后单击验证。
我收到一个错误,说AddAjaxPeriod
请求departureDate
非可选参数并且未填写...我可以同意,我通过匿名方法填写elementId
,formuleId
设置在postData
中,但departureData
和price
是用户尝试添加的形式。有没有办法获取该"添加表单"(日期和价格)的值并将它们传递给AddAjaxPeriod
方法?
编辑:
在 Oleg 备注之后,我找到了网格初始化(在父部分视图中发生)。这是代码:
jQuery("#periode-grid").jqGrid({
"url": '@Url.Action("PeriodePagedList", new { elementId = ViewData["ProductId"] })',
"datatype": 'local',
"mtype": 'POST',
"width": 400,
"height": 100,
"colNames": [
"@Resources.Catalog_Products.FormulePeriode_DepartureDate",
"@Resources.Catalog_Products.FormulePeriode_Price",
"" // Actions
],
"colModel": [
{ "name": 'DepartureDate', "index": 'DepartureDate', "editable": true, "align": 'center', "width": 100, "sorttype": 'date', "datefmt": 'dd/mm/yyyy', "editoptions": { "dataInit": function (element) { jQuery(element).datepicker({ "dateFormat": 'dd/mm/yy', "minDate": 0, "showAnim": '' }) } }, "editrules": { "required": true, "date": true } },
{ "name": 'Price', "index": 'Price', "editable": true, "align": 'center', "editrules": { "required": true }, "width": 100, "formatter": currencyFormatter, "unformat": unformatCurrency },
{ "name": 'Actions', "index": 'Actions', "width": 50, "align": 'center', "search": false, "sortable": false }
],
"sortname": 'DepartureDate',
"rowNum": 100,
"loadComplete": OnLoadPeriodeComplete,
"pager": jQuery('#periode-pager'),
"pginput": false,
"pgbuttons": false,
"viewrecords": false,
"imgpath": '@Url.Content("~/Content/jqueryui/images")',
"caption": "@Resources.Catalog_Products.FormulePeriode_GridTitle",
"shrinkToFit": true,
"hidegrid": false
});
我不确定我是否理解您的所有问题。您在问题文本中没有发布足够的细节,所以我必须猜测一下。
首先,如果在创建网格后需要多次更改jqGrid的某些参数,通常使用setGridParam
方法。另一方面,重要的是要了解一个人只能创建网格(使用jQuery("#periode-grid").jqGrid({...});
)或添加导航器(使用jQuery("#periode-grid").jqGrid("navGrid", ...);
)。在创建网格期间,初始空<table id="periode-grid">
将被转换为相对复杂的div 和表结构(例如,在答案中简要描述)。因此,创建已创建网格的第二次尝试将无助于任何操作。以同样的方式,navGrid
的第一次调用将创建网格的导航栏。如果导航栏已存在,则调用navGrid
的第二次尝试将不执行任何操作。
因此,我发现非常怀疑将setGridParam
和navGrid
的调用组合在一起,因为一个只能调用一次,而另一个通常会被调用多次。
如果您需要向服务器发送一些额外的参数(如formuleId
),我建议您使用postData
属性的函数形式。所以你可以使用以下选项直接创建jqGrid
jQuery("#periode-grid").jqGrid({
url: "someUrl",
postData: {
formuleId: function () {
var someValue;
// evaluate the CURRENT value of parameter which you need to sent
// you can use someValue=$("#someControl").val(); for example
...
return someValue;
}
}
在填充网格期间使用的formuleId
值将始终为当前值。
在编辑期间 jqGrid 将id
参数发送到服务器。这是网格的编辑行的行。所以你应该AddAjaxPeriod
方法的符号更改为
public JsonResult AddAjaxPeriod(string id, Guid elementId, Guid formuleId,
DateTime departureDate, double price)
如果您希望重命名id
参数,您可以使用网格prmNames
选项。例如,如果选项prmNames: { id: "myId" }
则可以使用
public JsonResult AddAjaxPeriod(string myId, Guid elementId, Guid formuleId,
DateTime departureDate, double price)