Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading a model into partial view from within a foreach loop

I want to display a partial view and populate it with the model Sale. Currently this is my (non-working) code:

Home view:

@foreach (Webshop.Models.Sale item in Model) {
    @Html.Partial("_Sale", (Webshop.Models.Sale) item)
}

Partial:

@model Webshop.Models.Sale

<div class="sale">
    <h5>@Html.ActionLink(item.Product.Name, "Details", "Product", new { id = item.ID }, null)</h5>
</div>

I get a compilation error saying item doesn't exist. If I name it model, it doesn't exist either.

  • Is using a partial view actually the way to achieve what I want?
  • What's the proper way of loading a model into a partial view from within my foreach loop?
like image 775
Scuba Kay Avatar asked Jul 25 '26 12:07

Scuba Kay


1 Answers

Change your partial code. item should be Model:

@model Webshop.Models.Sale
<div class="sale">
    <h5>@Html.ActionLink(Model.Product.Name, "Details", "Product", new { id = Model.ID }, null)</h5>
</div>
like image 132
gzaxx Avatar answered Jul 28 '26 02:07

gzaxx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!