ich diesen Code hatte und es funktionierte gut:ASP.NET MVC Zwei Modelle in ove Ansicht, Codeänderung
@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>
Aber ich brauchte ein weiteres Modell hinzuzufügen, ich getan, dass mit Tuple Auf diese Weise:
@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>
Aber jetzt habe ich ein Problem mit der unteren Hälfte des Codes. Von @foreach
Ich weiß nicht, was ich statt @foreach (var Artikel in Modell)
Und wird dann @ Html.DisplayFor (ModelItem =>Artikel setzen müssen. Titel) brauchen auch einige Änderungen.
Sorry für lange Post, versuchte ich, das Problem so gut wie ich kann zu erklären.
Der geeignete Weg, um mit dieser Situation umzugehen (benötigt mehr als ein Modell auf einer bestimmten Ansichtsseite), ist die Verwendung eines ViewModels, das die von Ihnen benötigten Modelle enthält. –
Ja. Wenn Sie die Modelle m1 und m2 für Ihre Ansicht verwenden möchten, erstellen Sie ein anderes Modell (vm) mit Mitgliedern für m1 und m2. – MKR