Search code examples
asp.netmany-to-manyentityfluent-interface

fluent table display/retrieve item from another model (Icollection) (related table) in html


I have 2 tables Actors, Movies

var Movie = new List<Movie>
            {
            new Movie{MovieID=1,Title="Chemistry",Date="2013-09-01", Budget=10000,  Actors = new List<Actor>() },
            new Movie{MovieID=2,Title="Chemistry Double",Date="2014-09-01", Budget=78600, Actors = new List<Actor>()}
            };

Entity automatically creates MovieActors table. Of course I fill info with

Movies.Actor.Add (Actor[3]); //specific number

And Fluen API creates corresponding MovieActor table. That works.

I have Actor/views Index.cshtml

How can I display specific movie in actors table?

I create decision but this shows nothing but actors.name

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
       </td>

        <td>
            @foreach (var subitem in item.Movies)
            {
            Html.DisplayFor(Movies => subitem.MovieID);
            } 
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
        @Html.ActionLink("Details", "Details", new { id=item.ID }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
`}

Solution

  •   Html.DisplayFor(Movies => subitem.MovieID); 
    

    I should add "@" character.

      @Html.DisplayFor(Movies => subitem.MovieID); 
    

    And all works. No logical errors was.