[RESOLVED]using tuple in a view

I’m using a tuple to use 2 models in 1 view but I have a problem in the Index view as for the loop foreach can’t loop in the model items to display them because the type of the view is not IEnumerable.

this is my view:

@model  Tuple<sportsMania.AD,sportsMania.ADS_imgs>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        
        <th>
            @Html.DisplayNameFor(model => model.Item1.location)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Item1.type)
        </th>
        <th></th>
    </tr>

@foreach (Tuple<sportsMania.AD, sportsMania.ADS_imgs> item in Model)
{
    <tr>
       
        <td>
            @Html.DisplayFor(modelItem => item.Item1.location)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Item1.type)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Item2.link)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Item2.url)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Item1.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Item1.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Item1.Id })
        </td>
    </tr>
}

</table>

this is my controller

 public ActionResult Index()
        {
            var first = new sportsMania.AD();
            var second = new sportsMania.ADS_imgs();
            return View(Tuple.Create(first, second));
        }

how can I display my data?

You don’t have a collection of data so what exactly are you trying to loop through?  Are AD and ADS_imgs collections?  Or can you just do this?

    <tr>
       
        <td>
            @Html.DisplayFor(modelItem => item.Item1.location)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Item1.type)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Item2.link)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Item2.url)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Item1.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Item1.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Item1.Id })
        </td>
    </tr>
// I dropped the for loop

When you use "var" instead of proper types it makes it a lot harder to understand what you are doing, or trying to do in your code.  It’s one of the reasons var is bad practice.

im trying to display the data in the ADs table as well as the data in the ADS-imgs table 

That is two separate collections, how do you intended to show them in one loop?  Do the indexes of the collections relate to each other?  ie does AD[2] relate to ADS-imgs[2]?  If so then do a for loop between 0 and ADS.Count – 1, in your loop you get the
data using

Model.ADS[index] and Model.ADS-imgs[index]

Hi lolo512,

lolo512

im trying to display the data in the ADs table as well as the data in the ADS-imgs table 

I suggest that you should union these data. If you are using the EF, you could include the child entity. If you are using the ADO.NET, you could use join (e.g. left join).

Best Regards

Starain

2. You are sending collections separately and trying to use them with union.

3. either do not use typed View or You simply create a view Model having one property of type AD and AD may have another property of type ADImgs

Leave a Reply