0

I try to pass 'regular' parameters type from an ajax call ($.post) from a view to a controller which is supposed to receive those parameters plus a List>.

I need this List cause those parameters will be different from model.types, and the method in the controller dispatch all params in a switch case to private methods.

I tried several jquery objects builds, JSON.stringify, the array with indexes in the string (I know I can't name columns explicitly... part of my question) which always ends in the List> to null in the backend method.

The 'params' are visible in xhr debug, but the parameter _options is always null (ref controller method).

Here is my vars and my ajax call :

 function initDocumentsPreviewsActions() {
        $('.documents-list-preview-action').on('click', function () {

            if (xhrPreviewLoad != undefined)
                xhrPreviewLoad.abort();

            $('#documents-preview').html('<div class="general-form-message-loader"></div>');
            documentCurrentIndex = $(this).data('btnindex');
            setDatatableRowActive();

            var documentType = $(this).data('documenttype');
            var adherentRessourceId = $(this).data('adherentressourceid');
            var annee = $(this).data('annee');
            var echeancier = $(this).data('echeancier');
            var destinataireid = $(this).data('destinataireid');
            var destinatairetypeid = $(this).data('destinatairetypeid');
            var emetteurid = $(this).data('emetteurid');
            var emetteurmandatid = $(this).data('emetteurmandatid');
            var trimestres = $(this).data('trimestres');
            var params = [
                { '_annee': '' + annee + '', '_echeancier': '' + echeancier + '', '_trimestres': '' + trimestres + '' }
            ];

xhrPreviewLoad = $.ajax({
                url: '/Documents/PreviewDocumentSingle',
                data: {
                    _documentType: documentType,
                    _adherentRessourceId: adherentRessourceId,
                    _annee: annee,
                    _destinataireId: destinataireid,
                    _destinataireType: destinatairetypeid,
                    _emetteurId: emetteurid,
                    _emetteurMandatId: emetteurmandatid,
                    _echeancier: echeancier,
                    _options: JSON.stringify(params)
                },
                dataType: 'json',
                type: 'POST'
            }).done(function (documentPartialView) {
                $('#documents-preview').html('<img src="' + documentPartialView.src + '"/>');
                xhrPreviewLoad = undefined;
            });
        });
}

xhd Parameters in Firefox debugger :

_documentType   AppelCotisation
_adherentRessourceId    836
_annee  2018
_destinataireId 11
_destinataireType   Syndicat
_emetteurId 16289
_emetteurMandatId   5986
_echeancier False
_options    [{"_annee":"2018","_echeancier":"False","_trimestres":"undefined"}]

Controller method :

[HttpPost]
public JsonResult PreviewDocumentSingle(string _documentType, int _adherentRessourceId, int _destinataireId, string _destinataireType, int _emetteurId, int _emetteurMandatId, List<KeyValuePair<string, string>> _options)
        {
//(_options = null)
//some code
 return JsonResult
} 

I already have done this a long time ago but I cannot put a hand or my brain on it. I'm sure it's a little thing.

I expect to get ALL my parameters correctly or being suggested to declare another List of a different type in the backend (and how to formalize it in front), or Array... well... any help is good to come.

But I'd really like to keep mus ctor in the backend as it is.

Bilaal Rashid
  • 828
  • 2
  • 13
  • 21
RizzCandy
  • 121
  • 9
  • You’re passing a `string` object when your server-side method expects a `List>` – Davide Vitali Mar 22 '19 at 22:05
  • Yes thx, what I'd like is to build my list of KeyValuePair Correctly in the ajax's data, do you know the best or proper way to ? – RizzCandy Mar 22 '19 at 22:09
  • you will need to submit a form directly through Ajax: see [this answer](https://stackoverflow.com/questions/26191774/submit-form-using-ajax-in-asp-net-mvc-4) – Davide Vitali Mar 23 '19 at 06:33

1 Answers1

0

I finally findout myself how to solve this simple problem by changing to a Dictionnaray instead of a List> in my controller's method parameters and build a simple js object like this :

        var options = {};
        options['_annee'] = annee;
        options['_echeancier'] = echeancier;
        options['_trimestres'] = trimestres;
RizzCandy
  • 121
  • 9