0

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>
`}
streamc
  • 676
  • 3
  • 11
  • 27

2 Answers2

0

You should edit code:

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>()}
        };

new List<Actor>() is null. Actors must be filled from Actor table. edit like this:

new Movie{MovieID=1,Title="Chemistry",Date="2013-09-01", Budget=10000,  Actors = db.Actors().tolist()}

db.Actors() is Actor table.

Ali Soltani
  • 9,589
  • 5
  • 30
  • 55
  • No. It is not null. I just reduced my post. I realize adding by Movies.Actor.Add (Actor); And this creates MovieActor table automatically through fluent API. But I can`t retrieve this info and get it to html. – streamc Jun 06 '16 at 10:29
  • No. I have no problem, with filling my movie table with some actor or actors in list form. I post it. Later I would post my specific code. Anyway, thanks.Problema is output list of movie actors with other data to html. – streamc Jun 06 '16 at 11:03
0
  Html.DisplayFor(Movies => subitem.MovieID); 

I should add "@" character.

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

And all works. No logical errors was.

streamc
  • 676
  • 3
  • 11
  • 27