-1

I have a controller with parameters of different types (model, array, string and int) Something like this.

public JsonResult UpdateDemandeur(Demandeurs ddeur, Photos vImg, Array vRev, Array vLangAut, int vLangMater)

I want to POST my data thru AJAX call and I used something of this kind:

                var json = JSON.stringify({
                    'ddeur': {
                        ID_Ville: xIDVille,
                        ID_Province: xIDProv,
                        CodePostal_Demandeur: xCPDdeur,
                        Tel1_Demandeur: xTel1Ddeur,
                        Tel2_Demandeur: xTel2Ddeur,
                        Tel3_Demandeur: xTel3Ddeur,
                        Courriel1_Demandeur: xCour1Ddeur,
                        Courriel2_Demandeur: xCour2Ddeur,
                        Courriel3_Demandeur: xCour3Ddeur,
                        Code_Conseiller: xCodeCons,
                        ID_SituationMatrimoniale: xIDSitMat,
                        ID_Sexe: xIDSexe,
                        Date_Naissance_Demandeur: xDteNais,
                        ID_TranchesRevenu: xRevDdeur,
                        ID_Occupation: xIDOcc,
                        ID_Scolarite: xIDScol,
                        ID_StatutLegal: xIDStatLegal,
                        ID_Communaute: xIDComm,
                        ID_SourceInformation: xIDSceInfo,
                        Handicape: xHandicape,
                        Reference: xRef,
                        Remarques_Demandeur: xRemDdeur,
                        Date_Cloture_Dossier: xDtClotureDos,
                        Actif_Inactif: xInActif,
                        Dte_Saisie: xDteSaisie,
                        UserId: xUsrID

                    },
                    'vImg': {
                        Nom_Table: 'Demandeurs',
                        Photo: resp
                    }
                  ...
                });

                $.ajax({
                    type: "POST",
                    url: "../Conseiller/UpdateDemandeur",
                    data: json,
                    dataType: "json",
                  ...

As you can see on the screenshot, my variables at the controller side come empty with no error... I have tried several things but it's been a headache since.

enter image description here

2
  • You have not set the necessary contentType: 'contentType: application/json,' option (and not related, but use url: '@Url.Action("UpdateDemandeur", "Conseiller")', - always use Url.Action() to generate the correct url) Commented Feb 5, 2017 at 5:26
  • And you might want to consider accepting answers in your previous question that solved you problems. Commented Feb 5, 2017 at 5:31

2 Answers 2

0

There's no need to stringify it.. just pass an array of objects like so:

var json = [
                {
                    ID_Ville: xIDVille,
                    ID_Province: xIDProv,
                    CodePostal_Demandeur: xCPDdeur,
                    Tel1_Demandeur: xTel1Ddeur,
                    Tel2_Demandeur: xTel2Ddeur,
                    Tel3_Demandeur: xTel3Ddeur,
                    Courriel1_Demandeur: xCour1Ddeur,
                    Courriel2_Demandeur: xCour2Ddeur,
                    Courriel3_Demandeur: xCour3Ddeur,
                    Code_Conseiller: xCodeCons,
                    ID_SituationMatrimoniale: xIDSitMat,
                    ID_Sexe: xIDSexe,
                    Date_Naissance_Demandeur: xDteNais,
                    ID_TranchesRevenu: xRevDdeur,
                    ID_Occupation: xIDOcc,
                    ID_Scolarite: xIDScol,
                    ID_StatutLegal: xIDStatLegal,
                    ID_Communaute: xIDComm,
                    ID_SourceInformation: xIDSceInfo,
                    Handicape: xHandicape,
                    Reference: xRef,
                    Remarques_Demandeur: xRemDdeur,
                    Date_Cloture_Dossier: xDtClotureDos,
                    Actif_Inactif: xInActif,
                    Dte_Saisie: xDteSaisie,
                    UserId: xUsrID

                },
                {
                    Nom_Table: 'Demandeurs',
                    Photo: resp
                }
              ...
            ];

            $.ajax({
                type: "POST",
                url: "../Conseiller/UpdateDemandeur",
                data: json,
                dataType: "json",
              ...
Sign up to request clarification or add additional context in comments.

4 Comments

Also, you didn't post the class definitions for the objects on the c# side, and I assumed that the members all match. That's important.
Hello Jay, Just did so, but still get same result. My controller parameters are not filled out
I will check the models, but I doubt, cause I tried each parameter by itself and I get the result I want.
Just guessing now, but try passing an array of objects: var data = [ { ddeur object goes here }, { vImg object goes here } ];
0

The only way I could by pass was to define all my parameters in the data part of the AJAX call. Like this:

 $.ajax({
                    type: "POST",
                    url: "../Conseiller/UpdateDemandeur",
                    data: {
                        Adresse_Demandeur: xAddDdeur,
                        ID_Ville: xIDVille,
                        ID_Province: xIDProv,
                        CodePostal_Demandeur: xCPDdeur,
                        Tel1_Demandeur: xTel1Ddeur,
                        Tel2_Demandeur: xTel2Ddeur,
                        Tel3_Demandeur: xTel3Ddeur,
                        Courriel1_Demandeur: xCour1Ddeur,
                        Courriel2_Demandeur: xCour2Ddeur,
                        Courriel3_Demandeur: xCour3Ddeur,
                        Code_Conseiller: xCodeCons,
                        ID_SituationMatrimoniale: xIDSitMat,
                        ID_Sexe: xIDSexe,
                        Date_Naissance_Demandeur: xDteNais,
                        ID_TranchesRevenu: xRevDdeur,
                        ID_Occupation: xIDOcc,
                        ID_Scolarite: xIDScol,
                        ID_StatutLegal: xIDStatLegal,
                        ID_Communaute: xIDComm,
                        ID_SourceInformation: xIDSceInfo,
                        Handicape: xHandicape,
                        Reference: xRef,
                        Remarques_Demandeur: xRemDdeur,
                        Date_Cloture_Dossier: xDtClotureDos,
                        Actif_Inactif: xInActif,
                        Dte_Saisie: xDteSaisie,
                        UserId: xUsrID,

                        Nom_Table: xTable,  //(varchar(50), not null)
                        Photo: null, //(varbinary(max)2147483647)
                        Nom_Fichier_Photo: resp, //(varchar(max), null)
                        Remarques_Photo: null,  //(varchar(max), null)

                    },
                    dataType: "json",...

All of my parameters type were thus correctly evaluated. I know this not coding(with this, I'm sure to encounter a bottle neck somewhere soon), so if someone can help sort this out, it'd be fun.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.