0

I have an existing project that I need to maintain, but this is first time I meet the jqGrid...

Mainly, I have a Product, that can have multiple Formules. Each Formule can contain multiple Periods. A Period is described by a DepartureDate and a Price.

This is the code that manages the Period's grid. Specially it adds the grid's navigator with the posibility of add Periods.

When adding a new row in the grid, the user fills-up a form containing 2 fiels: the DepartureDate and a Price corresponding to the newly created Period.

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

And this is my AddAjaxPeriod signature, containing 4 parameters, including the date and price:

[HttpPost]
[AjaxRequestOnly]
[Transaction]
public JsonResult AddAjaxPeriod(Guid elementId, Guid formuleId, 
                                DateTime departureDate, double price)
{ ... }

Now everything works fine untill I open the form to add the price and date, fill-in the requested date and price, and click Validate.

I obtain an Error saying that AddAjaxPeriod requests departureDate non-optional parameter and is not filled-in... I could agree, I fill-in the elementId via the anonymous method, the formuleId is set in the postData, but the departureData and price are in the form the the user tries to add. Is there a way to get the values of that "add form" (date and price) and pass them into the AddAjaxPeriod method?

EDIT:

After the Oleg remarks, I found the grid initialization (in occurence in an parent partial view). This is the code:

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
});
serge
  • 13,940
  • 35
  • 121
  • 205

1 Answers1

1

I'm not sure that I understand all your problems. You don't posted enough details in the text of your question, so I have to guess a little.

First of all one use setGridParam method typically if one need to change multiple times some parameter of jqGrid, after the grid is created. On the other side it's important to understand that one can create the grid (with jQuery("#periode-grid").jqGrid({...});) or add the navigator (with jQuery("#periode-grid").jqGrid("navGrid", ...);) only once. During creating of the grid the initial empty <table id="periode-grid"> will be converted in relatively complex structure of divs and tables (described shortly in the answer for example). So the second attempt to create already created grid will do nothing. In the same way the first call of navGrid will create navigator bar to the grid. The second attempt to call navGrid will just do nothing if the navigator bar already exist.

So I find very suspected combining calls of setGridParam and navGrid because one can be called only once and the another will be called typically multiple times.

If you need to send some additional parameter (like formuleId) to the server I would recommend you to use function form of properties of postData. So you can directly create jqGrid with the following option

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;
        }
    }

In the way the value of formuleId used during filling of the grid will be always the current one.

During editing jqGrid send id parameter to the server. It's the rowid of editing row of the grid. So you should change the signatire of AddAjaxPeriod method to

public JsonResult AddAjaxPeriod(string id, Guid elementId, Guid formuleId, 
                                DateTime departureDate, double price)

If you prefer to rename id parameter you can use prmNames option of the grid. For example if the option prmNames: { id: "myId" } then you can use

public JsonResult AddAjaxPeriod(string myId, Guid elementId, Guid formuleId, 
                                DateTime departureDate, double price)
Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks for answering. I edited my post adding some more detailed explanations. I need to recuperate the Date and Price from the form that permets to add a Period, to send them to the AddAjaxPeriod method. – serge Feb 20 '14 at 11:14
  • @Serge: Sorry, but I still don't understand what you do. You added **description** of what you try to implement instead of JavaScript code which *really* works. You wrote about "DepartureDate" and "Price" columns of the grid but you defined `AddAjaxPeriod` method with **4 parameters** `elementId`, `formuleId`, `departureDate` and `price`. How other parameter will be sent? What are the names of columns in the grid? I recommend you to use at the beginning `string` type instead of `DateTime`, `double` and `Guid` and verify that the parameter will be non-empty in `AddAjaxPeriod`. – Oleg Feb 20 '14 at 11:44
  • How other parameters will be sent? As we can see from the code, the `elementId` is sent directly via `new { elementId = @ViewData["ProductId"] }`, the `formuleId` is sent via `postData: { formuleId: rowid }`... So, I have just add in a way the date and price, both coming from the form that appears when I click the "Add" button on the grid. This Add button will call the function `AddAjaxPeriod` with that 4 params. – serge Feb 20 '14 at 13:38
  • @Serge: I don't understand your question. Moreover it's important to distinguish additional data which will be send during *filling of the grid* from the data sent *during editing of row of the grid*. The date and price which coming from **will be already sent** to the server, but the names of parameters depend on `colModel` of the grid which you don't posted. I suppose that the names of columns don't corresponds to the names of `AddAjaxPeriod` method. You can use [Fiddler](http://www.telerik.com/fiddler) or Developer Tools of IE (press F12 to start) to trace HTTP traffic. – Oleg Feb 20 '14 at 13:50
  • I found the "colModel", in a parent to that view. See my edits. – serge Feb 20 '14 at 15:43
  • 1
    @Serge: If you use `name: 'DepartureDate'` and `name: 'Price'`. So you should use the same names in `AddAjaxPeriod` (in the same case). So I recommend you to try `public JsonResult AddAjaxPeriod(string id, string DepartureDate, string Price) {...}` and validate that `id`, `DepartureDate` and `Price` will be initialized. – Oleg Feb 20 '14 at 16:07
  • Thanks a lot! The problem wasn't in the case, but in the type of parameters! Once set to string, the problem is gone! – serge Feb 20 '14 at 16:39