我有這個代碼,它工作得很好:ASP.NET MVC兩種型號奧雅納視圖,更改代碼
@model IEnumerable<Moviestore.Models.Movie>
@{
ViewBag.Title = "Index";
}
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Genre)
</th>
<th>
@Html.DisplayNameFor(model => model.Author)
</th>
<th>
@Html.DisplayNameFor(model => model.Year)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
if (item.IsDeleted == 0)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Author)
</td>
<td>
@Html.DisplayFor(modelItem => item.Year)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.MovieID }) |
@Html.ActionLink("Details", "Details", new { id = item.MovieID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.MovieID })
</td>
</tr>
}
}
</table>
但我還需要補充一個模式,我做到了與元組是這樣的:
@using Moviestore.Models;
@model Tuple<Movie, User>
@{
ViewBag.Title = "Index";
}
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(tuple => tuple.Item1.Title)
</th>
<th>
@Html.DisplayNameFor(tuple => tuple.Item1.Genre)
</th>
<th>
@Html.DisplayNameFor(tuple => tuple.Item1.Author)
</th>
<th>
@Html.DisplayNameFor(tuple => tuple.Item1.Year)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
if (item.IsDeleted == 0)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Author)
</td>
<td>
@Html.DisplayFor(modelItem => item.Year)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.MovieID }) |
@Html.ActionLink("Details", "Details", new { id = item.MovieID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.MovieID })
</td>
</tr>
}
}
</table>
但現在我有問題的下半部分的代碼。 從@foreach
我不知道我需要什麼,而不是投入的@foreach(VAR項模式)
,並會再@ Html.DisplayFor(modelItem =>項目。標題)也需要一些改變。
對不起,對於很長的文章,我試着盡我所能解釋問題。
處理這種情況(需要在特定視圖頁面上有多個模型)的適當方法是使用包含所需模型的ViewModel。 –
是的。如果要使用模型m1和m2在視圖中使用,則創建另一個模型(vm),該模型具有m1和m2的成員。 – MKR