Bonjour à tous,
Je me permets de venir à votre contact car je n'arrive pas à trouver la solution à mon problème. J'effectue une requête en Linq où je récupère un ccertain nombre de données que je formate en un tableau.
je souhaite donc afficher ce résultat dans ma vue par un foreach
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 var posts = (from p in db.Post join lp in db.LikePost on p.postID equals lp.postID into lps orderby p.Date descending, p.postID descending select new { postID = p.postID, postText = p.Text, postFirstname = p.User.Firstname, postLastname = p.User.Lastname, postDate = p.Date, postLikeNBR = ( from l in lps where l.Choice == 1 select l ).Count(), postLikeUser = ( from l in lps where l.userID == 1 select l.Choice ).Count(), postComments = ( from cm in db.Comment join lc in db.LikeComment on cm.commentID equals lc.commentID into lcs orderby cm.Date where cm.postID == p.postID select new { commentFirstname = cm.User.Firstname, commentLastname = cm.User.Lastname, commentDate = cm.Date, commentText = cm.Text, commentLikeNBR = ( from l in lcs where l.Choice == 1 select l.Choice ).Count(), commentLikeUser = ( from l in lcs where l.userID == 1 select l.Choice ).Count() } ) }).ToList(); ViewBag.posts = posts; return View();
et cela me renvoie donc sur ma page internet
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 <table> @foreach (var item in ViewBag.posts) { <tr> <td> @item </td> </tr> } </table>
mais je n'arrive à atteindre les objets un à un, j'ai essayé item["postID"] ou même item.postID mais cela ne fonctionne pas, quelqu'un aurait-il une idée?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 { postID = 2, postText = gthjkn, postFirstname = Axel, postLastname = Leynaert, postDate = 06/02/2013 18:50:00, postLikeNBR = 0, postLikeUser = 0, postComments = System.Data.Common.Internal.Materialization.CompensatingCollection`1[<>f__AnonymousType4`6[System.String,System.String,System.DateTime,System.String,System.Int32,System.Int32]] } { postID = 1, postText = dfghj, postFirstname = Axel, postLastname = Leynaert, postDate = 06/02/2013 18:50:00, postLikeNBR = 2, postLikeUser = 1, postComments = System.Data.Common.Internal.Materialization.CompensatingCollection`1[<>f__AnonymousType4`6[System.String,System.String,System.DateTime,System.String,System.Int32,System.Int32]] } { postID = 0, postText = azerty, postFirstname = , postLastname = , postDate = 06/02/2013 13:23:00, postLikeNBR = 0, postLikeUser = 0, postComments = System.Data.Common.Internal.Materialization.CompensatingCollection`1[<>f__AnonymousType4`6[System.String,System.String,System.DateTime,System.String,System.Int32,System.Int32]] }
Merci
Partager