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 :

Trouver les données en fonction de l'Id [Débutant]


Sujet :

ASP.NET MVC

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    septembre 2019
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Vosges (Lorraine)

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : septembre 2019
    Messages : 4
    Points : 6
    Points
    6
    Par défaut Trouver les données en fonction de l'Id
    Bonjour, J'ai fais un HttpPost qui me donne un Id :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     [HttpPost]
            public IActionResult PokemonDetails(int PokemonId)
            {
     
                return PartialView("_PokemonDetails");
            }
    et j'aimerais retrouver toutes les infos du pokemon en Fonction de l'id.

    voici 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
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
     public class PokemonModel
        {
            public PokemonModel()
            {
     
            }
     
            public int Id { get; set; }
     
     
            public string Name { get; set; }
     
     
            public string UsName { get; set; }
     
     
            public string JpName { get; set; }
     
     
            public string Type1 { get; set; }
     
     
            public string Type2 { get; set; }
     
     
            public int Rate { get; set; }
     
            public string Image { get; set; }
        }
    voici mon controller :
    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
     public class PokedexController : Controller
        {
            public IActionResult Index()
            {
                #region ListeDesPokemons
                var pokemonList = new List<PokemonModel>();
                var Id = 1;
                var Img = 1;
     
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Bulbizarre", UsName = "Bulbasaur(us)", JpName = "&#12501;&#12471;&#12462;&#12480;&#12493;(jp)", Type1 = "Plante", Type2 = "Poison", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/00" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Herbizarre", UsName = "Ivysaur(us)", JpName = "&#12501;&#12471;&#12462;&#12477;&#12454;(jp)", Type1 = "Plante", Type2 = "Poison", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/00" + Img++ + ".png" });
    #endregion
     
     var model = new PokemonViewModel();
     
                model.Pokemons = pokemonList;
     
                ViewBag.TotalPokemon1G = pokemonList.Count;            
     
                return View(model);
            }
    J'aimerai pouvoir retrouver le name, le rate, l'image, ect... en fonction de l'id retourné, merci .

  2. #2
    Membre expérimenté
    Homme Profil pro
    Développeur .NET
    Inscrit en
    février 2007
    Messages
    820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : février 2007
    Messages : 820
    Points : 1 488
    Points
    1 488
    Par défaut
    Salut,

    Le premier soucis que tu vas avoir c'est que ta liste de pokemen est cree dans la methode index, donc lors du post cette methode netant pas apellee tu ne vas pas pouvoir recupere le pokemon voulu.

    Voila ce que ca devrais donc etre (pour la methode index)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public class PokedexController : Controller
    {
      var pokemonList = new List<PokemonModel>{
        new PokemonModel() { Id = 1, Name = "Bulbizarre", UsName = "Bulbasaur(us)", JpName = "&#12501;&#12471;&#12462;&#12480;&#12493;(jp)", Type1 = "Plante", Type2 = "Poison", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/001.png" },
        new PokemonModel() { Id = 2, Name = "Herbizarre", UsName = "Ivysaur(us)", JpName = "&#12501;&#12471;&#12462;&#12477;&#12454;(jp)", Type1 = "Plante", Type2 = "Poison", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/002.png" }
      };
     
    public IActionResult Index()
    {
      var model = new PokemonViewModel();
      model.Pokemons = pokemonList;
      ViewBag.TotalPokemon1G = pokemonList.Count;            
      return View(model);
    }
    Puis du coup la methode post :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    [HttpPost]
    public IActionResult PokemonDetails(int PokemonId)
    {
      PokemonModel model = pokemonList.First(p=>p.Id == PokemonId);
      return PartialView("_PokemonDetails", model );
    }
    Tu auras peut etre besoin d'importer le namespace "System.Linq" dans ta classe.

  3. #3
    Futur Membre du Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    septembre 2019
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Vosges (Lorraine)

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : septembre 2019
    Messages : 4
    Points : 6
    Points
    6
    Par défaut Résolu !


    alors mon controller :
    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
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
     public IActionResult Index()
            {
                #region ListeDesPokemons
     
                var pokemonList = new List<PokemonModel>();
                var Id = 1;
                var Img = 1;
     
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Bulbizarre", UsName = "Bulbasaur(us)", JpName = "&#12501;&#12471;&#12462;&#12480;&#12493;(jp)", Type1 = "Plante", Type2 = "Poison", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/00" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Herbizarre", UsName = "Ivysaur(us)", JpName = "&#12501;&#12471;&#12462;&#12477;&#12454;(jp)", Type1 = "Plante", Type2 = "Poison", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/00" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Florizarre", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/00" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Salamèche", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/00" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Reptincel", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/00" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Dracaufeu", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/00" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Carapuce", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/00" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Carabaffe", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/00" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Tortank", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/00" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Chenipan", UsName = "Caterpie(us)", JpName = "&#12461;&#12515;&#12479;&#12500; (Kyatap&#299;)(jp)", Type1 = "Insecte", Type2 = "", Rate = 255, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Chrysacier", Rate = 120, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Papillusion", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Aspicot", Rate = 255, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Coconfort", Rate = 120, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Dardargnan", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Roucool", Rate = 255, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Roucoups", Rate = 120, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Roucarnage", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Rattata", Rate = 255, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Rattatac", Rate = 127, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Piafabec", Rate = 255, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Rapasdepic", Rate = 90, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Abo", Rate = 255, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Arbok", Rate = 90, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Pikachu", Rate = 190, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Raichu", Rate = 75, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Sabelette", Rate = 255, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Sablaireau", Rate = 90, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Nidoran &#9792;", Rate = 235, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Nidorina", Rate = 120, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Nidoqueen", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Nidoran &#9794;", Rate = 235, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Nidorino", Rate = 120, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Nidoking", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Melofee", Rate = 150, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Melodelfe", Rate = 25, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Goupix", Rate = 190, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Feunard", Rate = 75, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Rondoudou", Rate = 170, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Grodoudou", Rate = 50, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Nosferapti", Rate = 255, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Nosferalto", Rate = 90, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Mystherbe", Rate = 255, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Ortide", Rate = 120, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Rafflesia", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Paras", Rate = 190, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Parasect", Rate = 75, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Mimitoss", Rate = 190, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Aeromite", Rate = 75, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Taupiqueur", Rate = 255, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Triopikeur", Rate = 50, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Miaouss", Rate = 255, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Persian", Rate = 90, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Psykokwak", Rate = 190, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Akwakwak", Rate = 75, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Ferosinge", Rate = 190, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Colossinge", Rate = 75, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Caninos", Rate = 190, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Arcanin", Rate = 75, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Ptitard", Rate = 255, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Têtarte", Rate = 120, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Tartard", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Abra", Rate = 200, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Kadabra", Rate = 100, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Alakazam", Rate = 50, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Machoc", Rate = 180, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Machopeur", Rate = 90, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Mackogneur", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Chetiflor", Rate = 255, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Boustiflor", Rate = 120, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Empiflor", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Tentacool", Rate = 190, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Tentacruel", Rate = 60, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Racaillou", Rate = 255, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Gravalanch", Rate = 120, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Grolem", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Ponyta", Rate = 190, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Galopa", Rate = 60, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Ramoloss", Rate = 190, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Flagadoss", Rate = 75, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Magneti", Rate = 190, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Magneton", Rate = 60, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Canarticho", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Doduo", Rate = 190, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Dodrio", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Otaria", Rate = 190, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Lamantine", Rate = 75, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Tadmorv", Rate = 190, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Grotadmorv", Rate = 75, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Kokiyas", Rate = 190, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Crustabri", Rate = 60, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Fantominus", Rate = 190, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Spectrum", Rate = 90, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Ectoplasma", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Onix", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Soporifik", Rate = 190, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Hypnomade", Rate = 75, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Kraby", Rate = 225, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Krabboss", Rate = 60, Image = "https://eternia.fr/public/media/pokedex/artworks/0" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Voltorbe", Rate = 190, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Electrode", Rate = 60, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Noeunoeuf", Rate = 90, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Noadkoko", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Osselait", Rate = 190, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Ossatueur", Rate = 75, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Kicklee", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Tygnon", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Excelangue", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Smogo", Rate = 190, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Smogogo", Rate = 60, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Rhinocorne", Rate = 120, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Rhinoferos", Rate = 60, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Leveinard", Rate = 30, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Saquedeneu", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Kangourex", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Hypotrempe", Rate = 225, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Hypocean", Rate = 75, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Poissirene", Rate = 225, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Poissoroy", Rate = 60, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Stari", Rate = 225, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Staross", Rate = 60, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "M. Mime", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Insecateur", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Lippoutou", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Elektek", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Magmar", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Scarabrute", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Tauros", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Magicarpe", Rate = 255, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Leviator", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Lokhlass", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Metamorph", Rate = 35, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Evoli", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Aquali", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Voltali", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Pyroli", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Porygon", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Amonita", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Amonistar", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Kabuto", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Kabutops", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Ptera", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Ronflex", Rate = 25, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Artikodin ", Rate = 3, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Electhor ", Rate = 3, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Sulfura", Rate = 3, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Minidraco", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Draco", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Dracolosse", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Mewtwo ", Rate = 3, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
                pokemonList.Add(new PokemonModel() { Id = Id++, Name = "Mew ", Rate = 45, Image = "https://eternia.fr/public/media/pokedex/artworks/" + Img++ + ".png" });
     
                #endregion ListeDesPokemons
     
                #region ListeDesPokeballs
     
                var IdBall = 1;
     
                var pokeBallList = new List<PokeBallModel>();
                pokeBallList.Add(new PokeBallModel() { IdBall = IdBall++, NameBall = "Poké Ball", UsNameBall = "Poke Ball(us)", Bball = "1", DescriptionBall = "Un objet pareil à une capsule qui capture les Pokémon sauvages. Il suffit pour cela de le jeter comme une balle.", ImageBall = "https://www.pokepedia.fr/images/thumb/e/e2/Pok%C3%A9_Ball-RS.png/600px-Pok%C3%A9_Ball-RS.png" });
                pokeBallList.Add(new PokeBallModel() { IdBall = IdBall++, NameBall = "Super Ball", UsNameBall = "Great Ball(us)", Bball = "1.5", DescriptionBall = "Une Ball très performante dont le taux de réussite est supérieur à celui de la Poké Ball.", ImageBall = "https://vignette.wikia.nocookie.net/pokeball/images/a/aa/Super_Ball.png/revision/latest/scale-to-width-down/280?cb=20190714233349&path-prefix=es" });
                pokeBallList.Add(new PokeBallModel() { IdBall = IdBall++, NameBall = "Hyper Ball", UsNameBall = "Ultra Ball(us)", Bball = "2", DescriptionBall = "Une Ball ultraperformante dont le taux de réussite est supérieur à celui de la Super Ball.", ImageBall = "https://ih1.redbubble.net/image.242700931.2868/flat,800x800,070,f.u1.jpg" });
                pokeBallList.Add(new PokeBallModel() { IdBall = IdBall++, NameBall = "Master Ball", UsNameBall = "Master Ball(us)", Bball = "255", DescriptionBall = "Assurément la Ball la plus performante. Elle permet de capturer à coup sûr un Pokémon sauvage.", ImageBall = "https://www.trzcacak.rs/myfile/full/234-2349446_19-pokeball-picture-freeuse-stock-ball-pokemon-huge.png" });
     
                #endregion ListeDesPokeballs
     
                #region ListeDesStatuts
     
                var IdStatut = 1;
     
                var pokemonStatutList = new List<PokemonStatutModel>();
                pokemonStatutList.Add(new PokemonStatutModel() { IdStatut = IdStatut++, NameStatut = "Aucun", UsNameStatut = "None", BStatut = 1, DescriptionStatut = "Le pokémon n'est pas altéré", ImageStatut = "" });
                pokemonStatutList.Add(new PokemonStatutModel() { IdStatut = IdStatut++, NameStatut = "Brûlé", UsNameStatut = "Burned", BStatut = 1.5, DescriptionStatut = "Une brûlure réduit l'attaque du Pokémon de moitié.", ImageStatut = "https://cdn.imgbin.com/18/11/10/imgbin-burning-fire-XLL8XkHapQV5HZJHrDXxqEBK8.jpg" });
                pokemonStatutList.Add(new PokemonStatutModel() { IdStatut = IdStatut++, NameStatut = "Gelé", UsNameStatut = "Frozen", BStatut = 2, DescriptionStatut = "", ImageStatut = "" });
                pokemonStatutList.Add(new PokemonStatutModel() { IdStatut = IdStatut++, NameStatut = "Paralysé", UsNameStatut = "Paralysed", BStatut = 1.5, DescriptionStatut = "", ImageStatut = "" });
                pokemonStatutList.Add(new PokemonStatutModel() { IdStatut = IdStatut++, NameStatut = "Empoisonné", UsNameStatut = "Poisoned", BStatut = 1.5, DescriptionStatut = "", ImageStatut = "" });
                pokemonStatutList.Add(new PokemonStatutModel() { IdStatut = IdStatut++, NameStatut = "Endormi", UsNameStatut = "Sleeping", BStatut = 2, DescriptionStatut = "", ImageStatut = "" });
     
                #endregion ListeDesStatuts
     
                var model = new PokemonViewModel();
     
                model.Pokemons = pokemonList;
     
                var pokemonJson = JsonConvert.SerializeObject(pokemonList);
                TempData["pokemonListJson"] = pokemonJson;
                TempData.Keep("PokemonListJson");
     
                model.PokeBalls = pokeBallList;
     
                var pokeballJson = JsonConvert.SerializeObject(pokeBallList);
                TempData["pokeBallListJson"] = pokeballJson;
                TempData.Keep("pokeBallListJson");
     
                ViewBag.TotalPokemon1G = pokemonList.Count;
     
                return View(model);
            }
     
            [HttpPost]
            public IActionResult PokemonDetails(int pokemonId)
            {
                if (TempData["pokemonListJson"] != null)
                {
                    if (string.IsNullOrEmpty(TempData["pokemonListJson"].ToString()))
                    {
                        return null;
                    }
                }
     
                var pokemonListJson = TempData["pokemonListJson"].ToString();
                TempData.Keep("PokemonListJson");
     
                var pokemonList = JsonConvert.DeserializeObject<List<PokemonModel>>(pokemonListJson);
                var selectedPokemon = pokemonList.SingleOrDefault(p => p.Id == pokemonId);
     
                if (selectedPokemon != null)
                {
                    return PartialView("_PokemonDetails", selectedPokemon);
                }
     
                return null;
            }
    J'ai pris ma list de pokémon je l'ai converti au format Json.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    var pokemonJson = JsonConvert.SerializeObject(pokemonList);
    Je l'ai ensuite mis dans un TempData :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     TempData["pokemonListJson"] = pokemonJson;
                TempData.Keep("PokemonListJson");
    dans l'action PokemonDetails :

    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
     [HttpPost]
            public IActionResult PokemonDetails(int pokemonId)
            {
                if (TempData["pokemonListJson"] != null)
                {
                    if (string.IsNullOrEmpty(TempData["pokemonListJson"].ToString()))
                    {
                        return null;
                    }
                }
     
                var pokemonListJson = TempData["pokemonListJson"].ToString();
                TempData.Keep("PokemonListJson");
     
                var pokemonList = JsonConvert.DeserializeObject<List<PokemonModel>>(pokemonListJson);
                var selectedPokemon = pokemonList.SingleOrDefault(p => p.Id == pokemonId);
     
                if (selectedPokemon != null)
                {
                    return PartialView("_PokemonDetails", selectedPokemon);
                }
     
                return null;
            }
    Je vérifie si la list de pokemon n'est pas vide :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    if (TempData["pokemonListJson"] != null)
                {
                    if (string.IsNullOrEmpty(TempData["pokemonListJson"].ToString()))
                    {
                        return null;  //On peux ici faire un retour d'un message d'erreur.
                    }
                }
    je transforme la liste qui est au format json en chaines de caractères : Je ne comprend pas pourquoi il faut la mettre en ToString() merci de m'éclairer sur ce point svp.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    var pokemonListJson = TempData["pokemonListJson"].ToString();
    ensuite je la Deserialized :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    var pokemonList = JsonConvert.DeserializeObject<List<PokemonModel>>(pokemonListJson);
    ensuite je récupère les information de mon pokemon en fonction de l'id provenant du HttpPOst (pokemonId) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    var selectedPokemon = pokemonList.SingleOrDefault(p => p.Id == pokemonId);
     
                if (selectedPokemon != null)
                {
                    return PartialView("_PokemonDetails", selectedPokemon);
                }
     
                return null; //Ici message d'erreur.
    et dans ma vu partiel j'ai mis :
    Code CSHTML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    @model PokemonModel
     
    @Html.DisplayFor(model => model.Id)  pour avoir l'id
    @Html.DisplayFor(model => model.UsName)  pour avoir le UsName.
    etc....

    Je n'ai juste pas compris pourquoi il falait faire ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    var pokemonListJson = TempData["pokemonListJson"].ToString();
    et ensuite cela :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     var pokemonList = JsonConvert.DeserializeObject<List<PokemonModel>>(pokemonListJson);

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

Discussions similaires

  1. filtrer les données en fonction de l'utilisateur
    Par aba_tarn dans le forum Requêtes et SQL.
    Réponses: 4
    Dernier message: 03/07/2008, 12h28
  2. Réponses: 3
    Dernier message: 13/03/2008, 10h40
  3. Problème pour trouver les données commune dans une requête
    Par Winterrage dans le forum Langage SQL
    Réponses: 3
    Dernier message: 08/02/2008, 11h14
  4. [XSL] Trier les données en fonctions des parametres?
    Par titoff85 dans le forum XSL/XSLT/XPATH
    Réponses: 6
    Dernier message: 11/08/2007, 09h49
  5. Filtrer les données en fonction d'un paramètre multivalué
    Par v1nce dans le forum SAP Crystal Reports
    Réponses: 1
    Dernier message: 18/01/2007, 11h10

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