2017-07-07 14 views
0

我有這個代碼,它工作得很好: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 =>項目。標題)也需要一些改變。

對不起,對於很長的文章,我試着盡我所能解釋問題。

+1

處理這種情況(需要在特定視圖頁面上有多個模型)的適當方法是使用包含所需模型的ViewModel。 –

+0

是的。如果要使用模型m1和m2在視圖中使用,則創建另一個模型(vm),該模型具有m1和m2的成員。 – MKR

回答

1

正如我在評論中提到的那樣,處理這種情況(在特定視圖頁面上需要多個模型)的適當方法是使用包含所需模型的ViewModel(請參閱here for reference)。

但是,要回答您的具體問題,您可以通過其位置訪問元組的每個項目,例如,如果你的模型是Tuple<Movie, User>,那麼你就可以訪問由model.Item1電影對象,並通過model.Item2用戶對象等

但我強烈建議你把我聯繫,而不是視圖模型方法。

0

你好這裏是你的要求視圖模型:

public class MovieUserViewModel 
{ 
    public IEnumerable<Movie> Movie { get; set; } 
    public IEnumerable<Users> Users { get; set; } 
} 

使用上述視圖模型,你可以輕鬆地訪問每個類

@foreach (var item in Model.Movie) 

@foreach (var item in Model.Users) 

附加信息:Understanding ViewModel in ASP.NET MVC

感謝

Karthik