table display/retrieve 来自另一个模型(Icollection)(相关 table)的项目 html

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

我有 2 table 位演员、电影

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

实体自动创建 MovieActor table。 当然我用

填写信息
Movies.Actor.Add (Actor[3]); //specific number

并且 Fluen API 创建相应的 MovieActor table。行得通。

我有Actor/viewsIndex.cshtml

如何在演员中显示特定电影table?

我做出了决定,但这只显示 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>
`}

您应该编辑代码:

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>() 为空。 Actor 必须由 Actor table 填充。像这样编辑:

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

db.Actors() 是演员 table.

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

我应该添加“@”字符。

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

一切正常。没有逻辑错误。