C
C
cats722017-12-06 11:13:58
ASP.NET
cats72, 2017-12-06 11:13:58

How to display request data in View ASP?

Good afternoon, faced such a problem, there is the following controller

DataClasses1DataContext context = new DataClasses1DataContext();

            var sostav = context.Teams.Join(context.Transfers,
                team=>team.TeamId,
                transfer => transfer.TeamId,
                (team, transfer) => new { nClub = team.nameClub, idPlayer = transfer.PlayerId });
            ViewBag.sost = sostav.ToList();

And the following view:
@foreach (var s in ViewBag.sost)
{

    <tr>
        <th>@s.nClub </th>
   </tr>
}

However, when loading the page, it gives an error
5a27a625bf3e2895787623.jpeg
But if you change the view to the following
@foreach (var s in ViewBag.sost)
{

    <tr>
        <th>@s </th>
   </tr>
}

Then the output will be something like this:
{ nClub = Spartak , idPlayer = 1 } { nClub = Spartak , idPlayer = 2 }
Tell me what's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kovalsky, 2017-12-06
@dmitryKovalskiy

Because the data exchange should take place through typed View and models, not ViewBag. If you feel like using ViewBag - cast to specific types and refer to properties. In your case, this will not work - because you were too lazy to create classes and used anonymous hacks. As a result - slightly hurt his forehead.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question