Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two queries, one view

The goal

Iterating between two queries within a single view.

The problem

In my application there is a comparison list of a specific product. At the top of the page, there is details of this product as its name, min/max price, photo and some other details.

What I need is something like this:

@modelComparisonList List<MyApp.Models.Products>
@modelProduct MyApp.Models.Products

@{
    ViewBag.Title = "Comparing " + modelProduct.name;
}

<h1>There is @modelProduct.quantity products to compare</h1>   

<table>
    @foreach (var item in modelComparisonList)
    {
        <tr>
            <p>@item.productName</p>
        </tr>
        <tr>
            <p>@item.productPrice</p>
        </tr>
        <tr>
            <p>@item.marketName</p>
        </tr>
    }
</table>

Can you understand my case?

I don't know how to perform a solution to resolve this. Can someone give me an idea?

like image 514
Guilherme Oderdenge Avatar asked Jan 20 '26 11:01

Guilherme Oderdenge


1 Answers

Just make a wrapper class that contains both, ie simply:

public class TheViewModel
{
    public List<MyApp.Models.Products> Item1 { get; set; }
    public MyApp.Models.Products Item2 { get; set; }
}
like image 166
McGarnagle Avatar answered Jan 22 '26 02:01

McGarnagle