asp.net mvc - C# MVC ViewBag error -


i'm new c# mvc please patient. i'm having trouble displaying output viewbag. application fantasy football webpage have 3 tables (right now, more come) 1 basic player info (dbo.player), 1 player background (dbo.playerbackground), , 1 definition table team (dbo.team).

in 1 of pages have name search , search position , want return information across these 3 tables.

public actionresult index() {     var players = (from p in db.players                    join pb in db.playerbackgrounds on p.playerid equals pb.playerid                    join t in db.teams on p.teamabbre equals t.teamabbre                    select new { playerid = p.playerid, playername = p.name, position = p.position,                                 height = pb.height, weight = pb.weight, college = pb.college, dob = pb.dob,                                 imageurl = pb.imageurl, years = pb.years,                                 teamname = t.name                                 }).tolist();      viewbag.data = players;     return view(); } 

the query works fine in index.cshtml keep getting errors.

@foreach (var player in viewbag.data) {     <tr class="success ui-dragable playerrow"  style="display: none;">         <td>             <input type="checkbox" />         </td>         <td>             @html.actionlink( (string)player.playername, "details", new { id = player.playerid }, new { @class = "detailslink" })         </td>         <td>             @player.teamname         </td>         <td>             @player.position         </td>.... 

from research i've done, seems should work. i've tried both , without (string) cast. without cast gives me red squiggly saying should cast , when get:

exception details: microsoft.csharp.runtimebinder.runtimebinderexception: 'object' not contain definition 'playername'

as step through, can watch player , has various properties should. idea i'm doing wrong?

the problem using anonymous object. can't take credit finding though, answer found right here (go give him up-vote). refer complete details.

essentially, short of story anonymous objects emitted internal compiler. causes problems when trying use them razor views because compiled separate assembly asp.net runtime (internal allows access in same assembly).

so, solution define view model:

public class playerviewmodel {     // replace actual type of playerid     public int playerid { get; set; }     // etc... } 

and use in controller:

public actionresult index() {     var players = (from p in db.players                    join pb in db.playerbackgrounds on p.playerid equals pb.playerid                    join t in db.teams on p.teamabbre equals t.teamabbre                    select new playerviewmodel { playerid = p.playerid, ... }).tolist();      return view(players); // use strongly-typed model property view                           // instead of viewbag.data (it's recommended) } 

and in view:

@* @ beginning of view *@ @model ienumerable<playerviewmodel> ... @foreach (var player in @model)     {          <tr class="success ui-dragable playerrow"  style="display: none;">             <td>                 <input type="checkbox" />             </td>             <td>                 @html.actionlink(player.playername, "details", new { id = player.playerid }, new { @class = "detailslink" })             </td>             <td>                 @player.teamname             </td>             <td>                 @player.position             </td>.... 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -