asp.net mvc Join two tables to a single list item in html unordered list

asp.net asp.net-mvc-5 entity-framework entity-framework-6 join

Question

I have successfully used an sql inner join with two tables in webforms to join two tables to a single html unordered list.

I am using Asp.net MVC 5 Entityframework 6

I am trying to join the same two tables in MVC to again output a single html unordered list.

The first table is named team with the column id the second table is named teamgallery with a column teamID. The values of team.id and teamgallery.teamID are equal.

in the controller:

dynamic dmodel = new ExpandoObject();
dmodel.Teamre = (from pd in db.teams
                join od in db.teamgalleries on pd.id equals od.teamID
                where pd.Catresales == "yes"
                select new 
                {
                    pd.id,
                    pd.name,
                    od.picture
                 }).ToList();
return View(dmodel);

In the view:

<ul>
    @foreach (var teamre in Model.Teamre)
    {
        <li>
            @teamre
        </li>
    }
</ul>

In the web browser this returns a list like this:

{ id = 1, name = John Doe, picture = picture1.jpg }

{ id = 2, name = Jane Doe, picture = picture2.jpg }

If I change the razor code to display just the names:

@teamre.name

I'll receive this error

Server Error in '' Application. 'object' does not contain a definition for 'name'

The error shows even if I write

name = pd.name, 
1
0
9/8/2015 12:41:09 PM

Accepted Answer

Thank you, adding a ViewModel for the name and picture solved the issue.

    public string picture { get; set; }
    public string name { get; set; }
0
9/13/2015 9:54:29 PM

Popular Answer

<ul>
@foreach (var teamre in dmodel.Teamre)
{
    <li>
        @teamre.name
    </li>
}

I think your using the wrong collection to iterate through...see my code change above.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow