首先我有一個電影設置從一個數據庫在本地主機/電影上工作良好的列表 但是我想更進一步,所以我創建了一個演員頁面,我現在列出所有的演員到(自己只是他們的名字) 我想要做的是你有例子 -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>
}
那麼你有問題嗎? – arserbin3