var BalliStudnets = from balli in contextDB.tableBalli.ToList() where balli.ID_Gruppi == Grupp_ID && balli.ID_Disciplini == Disciplina_ID join s in contextDB.tableStudents on balli.ID_Studenta equals s.ID select new { Name = s.Name, Pos1 = balli.Pos1, Tek1 = balli.Tek1, Rub1 = balli.Rub1, Pos2 = balli.Pos2, Tek2 = balli.Tek2, Rub2 = balli.Rub2, Samost = balli.Samost, Dosdacha = balli.Dosdacha,Premial=balli.Premial,Itog=balli.Itog }; ViewBag.Vedomost = BalliStudnets; 

When passing the BalliStudnets object via ViewBag to a view, the compiler curses that there is no Name property. He does not see the properties. Is there an error in the request? If, then what can it be?

 @foreach (var b in ViewBag.Vedomost) { <tr> <td>@i</td> <td style="text-align: left; padding:3px;"> @b.Name </td> <td>@b.Pos1</td> <td>@b.Tek1</td> <td>@b.Rub1</td> <td>@b.Pos2</td> <td>@b.Tek2</td> <td>@b.Rub2</td> <td>@b.Samost</td> <td>@b.Dosdacha</td> <td>@b.Itog</td> <td>@b.Premial</td> @{i++;} </tr> } 

Error itself

  • How is this object used in view? - Grundy
  • Like an array for foreach. - Vivus
  • add this use to the question - Grundy
  • try replacing var b with dynamic b - Grundy
  • Made. All the same, the same thing. - Vivus

1 answer 1

Create a class with a description of all the properties of your model in the Models folder.

 public class Vedomost{ public string Name {get; set;} public string Pos1 { get; set; } public string Tek1 { get; set; } public string Rub1 { get; set; } public string Pos2 { get; set; } //etc... } 

Get the results:

 var BalliStudnets = from balli in contextDB.tableBalli.ToList() where balli.ID_Gruppi == Grupp_ID && balli.ID_Disciplini == Disciplina_ID join s in contextDB.tableStudents on balli.ID_Studenta equals s.ID select new Vedomost // уже не анонимный класс { Name = s.Name, Pos1 = balli.Pos1, Tek1 = balli.Tek1, Rub1 = balli.Rub1, Pos2 = balli.Pos2, Tek2 = balli.Tek2, Rub2 = balli.Rub2, Samost = balli.Samost, Dosdacha = balli.Dosdacha,Premial=balli.Premial,Itog=balli.Itog }; 

In the view, you will have access to the properties of your model.

 @foreach (Vedomost b in ViewBag.Vedomost) { //etc... 

Anonymous types are class types that are derived from an object directly, and cannot be cast to any other type than an object. The compiler assigns a name for each anonymous type, even though it is not available for your application. From the perspective of the CLR, the anonymous type is no different from other reference types.

read MSDN here

  • Made. Everything worked out. Thank! - Vivus
  • Can you explain why he cursed? - Vivus
  • The result that was obtained after the select new was anonymous type, which has no structure. The compiler cannot see its properties. By creating a class Vedomost we created a data structure to access them. - Vivus
  • one
    If the answer helped you, mark it as “correct” (an asterisk below the answer rating), this will help the other users of the resource to understand the solution. Thank. - Erkin Mukhamedkulov
  • one
    @ Erkin Mukhamedkulov, an asterisk is to add to favorites, and ticked by is a tick - Grundy