MVC首先使用关系数据库

时间:2014-06-07 00:29:18

标签: model-view-controller relationships

首先,我有一个从本地主机/电影正常工作的数据库设置的电影列表 但是我想更进一步,所以我创建了一个Actors页面,我现在列出了所有的Actors(只有它们自己的名字) 我想做的就是你有 - 例如 -

Leonardo DiCaprio(id = 1) 我想查看页面以查看id是否与他匹配等(这已经完成,您将在下面的代码中看到) 接下来我想他所有的电影都会在桌子的另一排显示出来。正如你可以看到VIA这个截图 http://gyazo.com/ae193d80e7a39969116f76ab6568f38e.png 而不仅仅是他出演过的电影都出现了,正如你在下面我看到的那样,我在表格中创建了一个关系。电影& ActorsMovies,只是链接Ids

我有3个表设置,如下所示: 电影 - Id(PK), 名称

演员: ID(PK), 名称

ActorsInMovies: MovieId(PK), ActorId(PK)

Here is my controller:
public ActionResult Movies( int id )
{
   var model = MoviesViewModel(); //Create our model
   model.PageTitle = = actor.Name + "'s' Movies"; // set page title to actors name
   model.Actorname = actor.Name; // I do this to ensure the name always matches the id
   var items = db.Movies.OrderBy(i => i.Name).AsQueryable(); //Link items to Movies DB    and make it queryable ( as I want to use pagedLists later when its working )
    if (!String.IsNullOrEmpty(actor.Name)) //if name is not null
    {
         items = items.Where( a => a.Id == id );//I already know this is wrong but I dont know what the correct process is I think the theory part behind it I understand, here I need to check the relationships to ensure that the actor matches with the movies I just am unsure how to do it.
    }

        model.MovieList = items.ToList();
        return View(model);         
}

我的观点只是一个带有foreach循环的普通表(我要删除Html,所以它不会弄乱:

    @foreach (var item in Model.MovieList)
    {               
        <tr>
            <td>@Html.DisplayFor(modelItem => Model.Actorname)</td>
            <td>
                <!-- MS:This part populates the table row-->
                @Html.DisplayFor(modelItem => item.Name)
            </td>

            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.Id }, new { @class = "btn btn-default" })
                @Html.ActionLink("Details", "Details", new { id = item.Id }, new { @class = "btn btn-default" })
                @Html.ActionLink("Delete", "Delete", new { id = item.Id }, new { @class = "btn btn-danger" })
            </td>
        </tr>
    }

1 个答案:

答案 0 :(得分:0)

在MVC中映射多对多关系以及限制数据库查询数量的最简单方法如下所示。 这也是分页,因为你提到了这个愿望。

MovieActor架构:(电影和演员的多对多地图)

  • ID(PK)
  • ActorID(FK to Actor.ID)
  • MovieID(FK to Movie.ID)
  • CharacterName

<强>控制器:

const int Rows = 50; // put this wherever appropriate, or customize and save in Session

/// <summary>
/// This shows all the Movies the given Actor was in.
/// </summary>
/// <param name="id">Actor ID</param>
/// <param name="page">Page of Movies to display, starting at 1</param>
public ActionResult Movies(int id, int page = 1)
{
    // get the actor
    Actor actor = MyDataContext.GetActorByID(id); // one database call

    // get the actor's movies
    List<Movie> movies = MyDataContext
        .GetMoviesByActorID(actor.id) // no database call
        .Skip((page - 1) * Rows)
        .Take(Rows); // one database call

    // build the view model
    // NOTE: could move this into a constructor MoviesViewModel(actor, movies)
    var viewModel = MoviesViewModel
    {
        PageTitle = actor.Name + "'s Movies",
        ActorName = actor.Name,
        Movies = movies
    };

    // show the view
    return View(model);         
}

<强>型号:

public partial class MyDataContext
{
    public Actor GetActorByID(int id)
    {
        return Actors.FirstOrDefault(x => x.ID == id)
    }

    public IQueryable<List<Movie>> GetMoviesByActorID(int actorID)
    {
        return Movies.Where(x => x.MovieActor.ActorID == actorID);
    }
}

视图与普通IEnumerableList一样。