Home ASP.NET MVC - Display one item from two
Reply: 0

ASP.NET MVC - Display one item from two

FreeStyle09
1#
FreeStyle09 Published in 2018-01-10 17:25:25Z

1) I have 2 tables: LaptopProduct and LaptopPhotos.

2) LaptopPhotos has foreign key LaptopProductID to table LaptopProduct (one to many)

3) I added 15 records to LaptopProduct, two of them have column Promotion set to some value

4) I want to load these laptops on the page to one div and randomly display them:

var laptopproducts = db.LaptopProducts.Where(c => c.Promotion != 0).Take(1).OrderBy(g => Guid.NewGuid()).ToList();

5) Later to these laptops i want to load only first photo from table LaptopPhotos which meets the condition:

if (laptopproducts.ID == photos.LaptopProductID)
                     {
                         <div>
                            <img src="~/Content/LaptopProduct/@photos.IconFileName" />
                         </div>
                     }

Code above means: Load these photos ex. first loaded is LaptopProductID == 5 because it has Promotion !=0 and i set from second table same condition that it's loaded to load all photos from table LaptopPhotos on record LaptopProductID == 5 which are 4 photos. And i want to load only FIRST from this table.

Only 5-1.png i want to load.

Index.cshtml:

@foreach (var laptopproducts in Model.LaptopProducts)
                {
                    foreach (var photos in Model.LaptopPhotos)
                    {
                        if (laptopproducts.ID == photos.LaptopProductID)
                        {
                            <div>

                                <img src="~/Content/LaptopProduct/@photos.IconFileName" />
                            </div>
                        }

                    }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.299625 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO