IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

ASP.NET MVC Discussion :

comment binder un json envoyer par la méthode Post (jquery)


Sujet :

ASP.NET MVC

  1. #1
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut comment binder un json envoyer par la méthode Post (jquery)
    bonsoir,

    aujourd'hui j'essaie de faire passer un objet typé par la méthode Post de jquery.

    mon model :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
     
    namespace myPortfolio.Areas.admin.Models
    {
        public class SkillModel
        {
            public Guid Id { get; set; }
            public string Name { get; set; }
        }
    }
    mon code jquery :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    var data = {
        pictures: [
            { Id: '67f64a95-45cc-40bb-ac81-6247e4105953', Url: '1.png', Main: false },
            { Id: '4841f169-1b5b-47cc-9f79-e31b71d67d12', Url: '2.png', Main: false },
            { Id: 'af3d3aea-5f6c-4c4d-93b5-bed09cbfb4fc', Url: '3.png', Main: false }
        ]
    };
     
    json = JSON.stringify(data);
     
    $.post("/admin/ManageProject/SaveArticle/", json);
    mon code asp.net :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    [ValidateInput(false)]
    [AcceptVerbs(HttpVerbs.Post)]
    [HttpPost]
    public void SaveArticle(IList<PhotoModel> pictures)
    {
        ...
    }
    grace a un point d'arret, je vois que mon paramètre "pictures" n'est pas détecté comme étant une liste, il ne contient aucune ligne, il est totallement null. c'est la première fois que je fais ça, j'ai lu pas mal de chose sur le net, et je pensais que ça allait se faire tout seul. apparemment il y a un binder par défaut O_o.

    Où ai-je commis une erreur ?

    merci

  2. #2
    Membre habitué Avatar de mikoukoumi
    Homme Profil pro
    Développeur .net
    Inscrit en
    Mai 2010
    Messages
    151
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Développeur .net
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2010
    Messages : 151
    Points : 169
    Points
    169
    Par défaut
    Bonjour,

    Serait-il possible d'avoir le message d'erreur???
    est ton ami, l'oubli pas...

  3. #3
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    pas de message d'erreur, mon paramètre est juste nul :/

  4. #4
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    bonjour,

    Je viens d'essayer quelque chose. j'ai retiré le json.stringify(), en résultat, ma fonction c# détecte "pictures" comme étant une collection, mais elle est vide :/

    je ne trouve toujours rien de concluant sur le net.

  5. #5
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    Youhouu, j'ai en parti réussi !

    voici mon code jquery :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    $.ajax({
                    url: "/admin/ManageProject/SaveArticle/",
                    type: 'POST',
                    traditional: true,
                    data: {
                        Id: '67f64a95-45cc-40bb-ac81-6247e4105953',
                        Url: '1.png',
                        Main: false
                    }
                });
    là ça fonctionne, par contre je n'arrive pas à envoyer une collection !!
    je vais aussi devoir envoyer plusieurs objets :
    - deux collections de type différent.
    - un objet.

  6. #6
    Rédacteur
    Avatar de Nathanael Marchand
    Homme Profil pro
    Expert .Net So@t
    Inscrit en
    Octobre 2008
    Messages
    3 615
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Expert .Net So@t
    Secteur : Conseil

    Informations forums :
    Inscription : Octobre 2008
    Messages : 3 615
    Points : 8 080
    Points
    8 080
    Par défaut
    Et avec un IEnumerable<T> ?

  7. #7
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    jquery :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    var data = [
                            { Id: '67f64a95-45cc-40bb-ac81-6247e4105953', Url: '1.png', Main: false },
                            { Id: '4841f169-1b5b-47cc-9f79-e31b71d67d12', Url: '2.png', Main: false },
                            { Id: 'af3d3aea-5f6c-4c4d-93b5-bed09cbfb4fc', Url: '3.png', Main: false }
                        ];
     
               $.ajax({
                    url: "/admin/ManageProject/SaveArticle/",
                    type: 'POST',
                    traditional: true,
                    data: JSON.stringify(data)
                });
    c# :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    public void SaveArticle(IEnumerable<PhotoModel> pictures)
    toujours null :/
    mais merci

  8. #8
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    j'ai trouvé un indice sur cette page http://dotnetslackers.com/articles/a...l-Binding.aspx.
    Figure 9: Receiving form data as a list of Employee objects

    je vais essayer ça.

    edit
    j'avance pas, ya moyen de lancer une bouée de sauvetage ?
    vous avez surement un bout de code qui traine quelque part et qui pourrait me sortir de là non ? ^^

  9. #9
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    ahhh
    je viens de faire un grand pas !

    voici mon code jquery (ça fonctionne) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    var pictures = [
                                    {"name":"pictures.Index","value":"0"},
                                    {"name":"pictures[0].Id","value":"67f64a95-45cc-40bb-ac81-6247e4105953"},
                                    {"name":"pictures[0].Url","value":"1.png"},
                                    {"name":"pictures[0].Main","value":"0"},
     
                                    {"name":"pictures.Index","value":"1"},
                                    {"name":"pictures[1].Id","value":"af3d3aea-5f6c-4c4d-93b5-bed09cbfb4fc"},
                                    {"name":"pictures[1].Url","value":"2.png"},
                                    {"name":"pictures[1].Main","value":"0"}
                                ];
     
                $.ajax({
                    url: "/admin/ManageProject/SaveArticle/",
                    type: 'POST',
                    traditional: true,
                    data: pictures
                });
    maintenant je passe à l'étape suivante, soit je veux faire passer plusieurs paramètres, soit je vais faire passer un objet contenant des collections.
    Les deux ne fonctionnent pas, mais je vais m’attarder sur la 2ème solution:
    (ne fonctionne pas ) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    var pictures = [
                                    {"name":"Pictures.Index","value":"0"},
                                    {"name":"Pictures[0].Id","value":"67f64a95-45cc-40bb-ac81-6247e4105953"},
                                    {"name":"Pictures[0].Url","value":"1.png"},
                                    {"name":"Pictures[0].Main","value":"0"},
     
                                    {"name":"Pictures.Index","value":"1"},
                                    {"name":"Pictures[1].Id","value":"af3d3aea-5f6c-4c4d-93b5-bed09cbfb4fc"},
                                    {"name":"Pictures[1].Url","value":"2.png"},
                                    {"name":"Pictures[1].Main","value":"0"}
                                ];
     
                var articleProject = {
                                        Id: $("#articleId").val(),
                                        Title: $("#articleTitle").val(),
                                        Text: $('#articleProjectEditor').val(),
                                        Begin: $('#articleBegin').val(),
                                        End: $('#articleEnd').val(),
                                        Url: $('#articleUrl').val(),
                                        Pictures: pictures
                                    };
     
                $.ajax({
                    url: "/admin/ManageProject/SaveArticle/",
                    type: 'POST',
                    traditional: true,
                    data: articleProject
                });
    c# :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    [ValidateInput(false)]
            [AcceptVerbs(HttpVerbs.Post)]
            [HttpPost]
            public void SaveArticle(ArticleProjectModel articleProject)
    mon objet articleProject est bien renseigné ! il est sensé contenir une collection "pictures", mais ici elle ne fonctionne pas :/
    pourtant je l'ai inséré de la même façon qu'au dessus, et là ça fonctionnait ...

  10. #10
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2007
    Messages
    696
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Décembre 2007
    Messages : 696
    Points : 222
    Points
    222
    Par défaut
    YES !!!

    bon, voici un petit exemple pour ceux qui sont dans le même cas que moi !

    voici 2 classes :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
     
    namespace myPortfolio.Areas.admin.Models
    {
        public class ArticleProjectModel
        {
            public Guid Id { get; set; }
            public string Title { get; set; }
            public string Text { get; set; }
            public string Begin { get; set; }
            public string End { get; set; }
            public string Url { get; set; }
            public List<PhotoModel> Pictures { get; set; }
        }
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
     
    namespace myPortfolio.Areas.admin.Models
    {
        public class PhotoModel
        {
            public string Id { get; set; }
            public string Url { get; set; }
            public string Main { get; set; }
        }
    }
    maintenant, soit on fait passer 2 paramètres dans la fonctions, soit on fait passer un paramètre ArticleProjectModel contenant une collection de PhotoModel.

    1 paramètre :

    jquery :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    var data = [
                                /* articleProject */
                                { "name":"articleProject.Id", "value":$("#articleId").val() },
                                { "name":"articleProject.Title", "value":$("#articleTitle").val() },
                                { "name":"articleProject.Text", "value":$("#articleProjectEditor").val() },
                                { "name":"articleProject.Begin", "value":$("#articleBegin").val() },
                                { "name":"articleProject.End", "value":$("#articleEnd").val() },
                                { "name":"articleProject.Url", "value":$("#articleUrl").val() },
     
                                /* Pictures */
                                { "name":"articleProject.Pictures.Index", "value":"0" },
                                { "name":"articleProject.Pictures[0].Id", "value":"67f64a95-45cc-40bb-ac81-6247e4105953" },
                                { "name":"articleProject.Pictures[0].Url", "value":"1.png" },
                                { "name":"articleProject.Pictures[0].Main", "value":"0" },
                                { "name":"articleProject.Pictures.Index", "value":"1" },
                                { "name":"articleProject.Pictures[1].Id", "value":"af3d3aea-5f6c-4c4d-93b5-bed09cbfb4fc" },
                                { "name":"articleProject.Pictures[1].Url", "value":"2.png" },
                                { "name":"articleProject.Pictures[1].Main", "value":"0" }
                            ];
     
                $.ajax({
                    url: "/admin/ManageProject/SaveArticle/",
                    type: 'POST',
                    traditional: true,
                    data: data
                });
    c# :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    [ValidateInput(false)]
            [AcceptVerbs(HttpVerbs.Post)]
            [HttpPost]
            public void SaveArticle(ArticleProjectModel articleProject)
    2 paramètres :

    jquery :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    var data = [
                                /* articleProject */
                                { "name":"articleProject.Id", "value":$("#articleId").val() },
                                { "name":"articleProject.Title", "value":$("#articleTitle").val() },
                                { "name":"articleProject.Text", "value":$("#articleProjectEditor").val() },
                                { "name":"articleProject.Begin", "value":$("#articleBegin").val() },
                                { "name":"articleProject.End", "value":$("#articleEnd").val() },
                                { "name":"articleProject.Url", "value":$("#articleUrl").val() },
     
                                /* Pictures */
                                { "name":"pictures.Index", "value":"0" },
                                { "name":"pictures[0].Id", "value":"67f64a95-45cc-40bb-ac81-6247e4105953" },
                                { "name":"pictures[0].Url", "value":"1.png" },
                                { "name":"pictures[0].Main", "value":"0" },
                                { "name":"pictures.Index", "value":"1" },
                                { "name":"pictures[1].Id", "value":"af3d3aea-5f6c-4c4d-93b5-bed09cbfb4fc" },
                                { "name":"pictures[1].Url", "value":"2.png" },
                                { "name":"pictures[1].Main", "value":"0" }
                            ];
     
                $.ajax({
                    url: "/admin/ManageProject/SaveArticle/",
                    type: 'POST',
                    traditional: true,
                    data: data
                });
    c# :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    [ValidateInput(false)]
            [AcceptVerbs(HttpVerbs.Post)]
            [HttpPost]
            public void SaveArticle(ArticleProjectModel articleProject, List<PhotoModel> pictures)
    sujet résolu

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Récupération des données envoyées par la méthode POST
    Par karamaster dans le forum Langage
    Réponses: 2
    Dernier message: 12/01/2015, 07h33
  2. Réponses: 1
    Dernier message: 17/05/2013, 13h34
  3. [PHP 5.3] Niveaux de protection des données envoyées par la méthode post
    Par bailamos dans le forum Langage
    Réponses: 1
    Dernier message: 24/03/2010, 20h30
  4. Réponses: 2
    Dernier message: 23/03/2008, 03h05
  5. [AJAX] Comment envoyer un fichier par la méthode post à l'aide d'ajax?
    Par othmane126 dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 12/03/2008, 12h00

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo