bonsoir,
je viens de débuter en C# Modèl view controller (Mvc2) sous Visual Studio 2010 SP1, et je voudrais savoir comment faire pour récupérer le résultat d'une requête Sql-Server(2008 R2) sous forme Array()?

voilà mon controller
Code c# : 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
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using MvcApplication3.Models;
using System.Dynamic;
 
namespace MvcApplication3.Controllers
{
    public class CategorieController : Controller
    {
        static List<CategorieModels> Categorie = new List<CategorieModels>();
        CategorieModels cat = new CategorieModels();
        //
        // GET: /Categorie/
        public ActionResult ListerCategorie()
        {
            int nbr=cat.Charger_donner().Length;
            for (int i = 0; i < nbr; i++)
            {
 
            }
                ViewData["Nbr"] = nbr;
            return View(Categorie.ToArray());
        }
        [HttpPost]
        public ActionResult ListerCategorie(CategorieModels cat)
        {
            if (ModelState.IsValid)
            {
                cat.Charger_donner();
                return RedirectToAction("Teste", "Categorie");
            }
            return View(cat); 
        }
        //**********//
 
 
        public ActionResult Index()
        {
            return View();
        }
 
        //
        // GET: /Categorie/Details/5
 
        public ActionResult Details(int id)
        {
            return View();
        }
 
        //
        // GET: /Categorie/Create
 
        public ActionResult Create()
        {
            return View();
        } 
 
        //
        // POST: /Categorie/Create
 
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
 
        //
        // GET: /Categorie/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }
 
        //
        // POST: /Categorie/Edit/5
 
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
 
        //
        // GET: /Categorie/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }
 
        //
        // POST: /Categorie/Delete/5
 
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}

puis le modèl :

Code c# : 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
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.SqlClient;
 
namespace MvcApplication3.Models
{
    public class CategorieModels
    {
        public CategorieModels Categorie {get;set;}
        [Required(ErrorMessage = "Id est obligatoire")]
        public string Id_cat { get; set; }
        [Required(ErrorMessage = "Titre est obligatoire")]
        public string Titre { get; set; }
        public string Date { get; set; }
        [Required(ErrorMessage = "Image est obligatoire")]
        public string Image { get; set; }
        public string Id_admin { get; set; }
        [Required(ErrorMessage = "Devise est obligatoire")]
        public string devis { get; set; }
        public string active_cat { get; set; }
        public string envoivMar { get; set; }
        public string group_repr { get; set; }
        public string id_adminp { get; set; }
        public string order_cat { get; set; }
        //
        public CategorieModels()
            {
                //Charger_donner();
            }
        public string Teste()
            {
                return "Bonjour";
            }
        public Array Charger_donner()
        {
            Connexion conn = new Connexion();
            DataTable dt = new DataTable();
            conn.OuvrirConnexion();
            System.Collections.ArrayList ListeCategorie = new System.Collections.ArrayList();
            SqlDataAdapter adapter = new SqlDataAdapter("select * from categorie order by Id_Cat desc  ", conn.retour());
            adapter.Fill(dt);
            foreach (DataRow dtrow in dt.Rows)
            {
                CategorieModels Categorie = new CategorieModels();
                Categorie.Id_cat = dtrow["Id_Cat"].ToString();
                ListeCategorie.Add(Categorie);
            }
            conn.FermerConnexion();
            return ListeCategorie.ToArray();
        }//fin chrger donner
    }//fin classe
}

View Associé :
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
 
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication3.Models.CategorieModels>>" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	ListerCategorie
</asp:Content>
 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
 
    <h2>Liste Categorie</h2>
    <h1><%= Html.Encode(ViewData["Nbr"])%></h1>
 
    <table>
        <tr>
            <th></th>
            <th>
                Id_cat
            </th>
            <th>
                Titre
            </th>
            <th>
                Date
            </th>
            <th>
                Image
            </th>
            <th>
                Id_admin
            </th>
            <th>
                devis
            </th>
            <th>
                active_cat
            </th>
            <th>
                envoivMar
            </th>
            <th>
                group_repr
            </th>
            <th>
                id_adminp
            </th>
            <th>
                order_cat
            </th>
        </tr>
 
    <% foreach (var item in Model)
       { %>
 
        <tr>
            <td>
                <%: Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %> |
                <%: Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })%> |
                <%: Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })%>
            </td>
            <td>
                <%= Html.Encode(item.Id_cat) %>
            </td>
            <td>
                <%:item.Titre %>
            </td>
            <td>
                <%: item.Date %>
            </td>
            <td>
                <%: item.Image %>
            </td>
            <td>
                <%: item.Id_admin %>
            </td>
            <td>
                <%: item.devis %>
            </td>
            <td>
                <%: item.active_cat %>
            </td>
            <td>
                <%: item.envoivMar %>
            </td>
            <td>
                <%: item.group_repr %>
            </td>
            <td>
                <%: item.id_adminp %>
            </td>
            <td>
                <%: item.order_cat %>
            </td>
        </tr>
 
    <% } %>
 
    </table>
 
    <p>
        <%: Html.ActionLink("Create New", "Create") %>
    </p>
 
</asp:Content>
j'ai affecté la valeur de nombre de lignes retournées par la fonction Charger_donner(), dans la variable ViewData["Nbr"] = nbr; et je l'ai affichée dans la view <%= Html.Encode(ViewData["Nbr"])%>), juste pour savoir si la liste est bien remplit ou non. ça m'affiche 303 et c'est exactement le nombre de lignes de ma table Categorie dans la Bdd, donc la liste est Bien Remplit et la fonction Charger_donner() fonctionne.

maintenant je me demande ce que je dois mettre comme code dans la boucle For de ActionResult ListerCategorie() pour remplire la View et l'afficher???
Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
public ActionResult ListerCategorie()
        {
            int nbr=cat.Charger_donner().Length;
            for (int i = 0; i < nbr; i++)
            {
               // je me plante ici
            }
                ViewData["Nbr"] = nbr;
            return View(Categorie.ToArray());
        }