2014-06-07 135 views
1

首先我有一個電影設置從一個數據庫在本地主機/電影上工作良好的列表 但是我想更進一步,所以我創建了一個演員頁面,我現在列出所有的演員到(自己只是他們的名字) 我想要做的是你有例子 -MVC首先使用關係數據庫

萊昂納多·迪卡普里奧(ID = 1) 我想查詢的頁面,看看是否ID匹配他等(這已經是完成,你會看到下面的代碼) 接下來,我想他所有的電影,他已經在另一行顯示在桌子上。正如你可以通過這個截圖中看到的只是他自己都曾經出演過電影 http://gyazo.com/ae193d80e7a39969116f76ab6568f38e.png 而是展現出來,因爲你可以看到下面我做表演員&電影& ActorsMovies之間的關係,只是鏈接標識

我有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);   
} 

,我的看法是隻是一個正常的表中(IM 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> 
    } 
+0

那麼你有問題嗎? – arserbin3

回答

0

在MVC中映射多對多關係並限制數據庫查詢數量的最簡單方法如下所示。 這也是分頁,因爲你提到了這個願望。

MovieActor架構:(多到許多電影的地圖和演員)

  • ID(PK)
  • 的actorId(FK到Actor.ID)
  • MovieID(FK到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