0

I had this code and it worked fine:

@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>

But I needed to add one more model, I done that with Tuple In this way:

@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>

But now I have problem with bottom half of code. From @foreach

I don't know what I need to put instead of @foreach (var item in Model)

And will then @Html.DisplayFor(modelItem => item.Title) also need some changes.

Sorry for long post, I tried to explain the problem as best as I can.

Marks R
  • 3
  • 2
  • 1
    The appropriate way to handle this situation (needing more than one model on a particular View page) is to use a ViewModel which contains the models you need. – C. Helling Jul 07 '17 at 21:18
  • Yes. If you want to use models m1 and m2 to use in your view then create another model (vm) which has got members for m1 and m2. – MKR Jul 07 '17 at 21:22

2 Answers2

1

As I mentioned in the comments, the appropriate way to handle this situation (needing more than one model on a particular View page) is to use a ViewModel which contains the models you need (see here for reference).

To answer your specific question, though, you can access each item of the Tuple by its position, e.g. if your model is Tuple<Movie, User>, then you can access the Movie object by model.Item1 and the User object by model.Item2, etc.

But I strongly recommend you take the ViewModel approach that I linked instead.

C. Helling
  • 1,394
  • 6
  • 20
  • 34
0

Hi here is the viewmodel for your requirement:

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

With the above viewmodel , you can easily access each class

@foreach (var item in Model.Movie)

@foreach (var item in Model.Users)

Additional info : Understanding ViewModel in ASP.NET MVC

Thanks

Karthik

Karthik Elumalai
  • 1,574
  • 1
  • 11
  • 12