Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You have:</p> <pre><code>foreach (var category in Model.Categories) </code></pre> <p>and then </p> <pre><code>@foreach (var product in Model) </code></pre> <p>Based on that view and model it seems that <code>Model</code> is of type <code>Product</code> if yes then the second <code>foreach</code> is not valid. Actually the first one could be the one that is invalid if you return a collection of <code>Product</code>. </p> <p><strong>UPDATE:</strong> </p> <blockquote> <p>You are right, I am returning the model of type Product. Also, I do understand what is wrong now that you've pointed it out. How am I supposed to do what I'm trying to do then if I can't do it this way?</p> </blockquote> <p>I'm surprised your code compiles when you said you are returning a model of <code>Product</code> type. Here's how you can do it:</p> <pre><code>@foreach (var category in Model) { &lt;h3&gt;&lt;u&gt;@category.Name&lt;/u&gt;&lt;/h3&gt; &lt;div&gt; &lt;ul&gt; @foreach (var product in category.Products) { &lt;li&gt; put the rest of your code &lt;/li&gt; } &lt;/ul&gt; &lt;/div&gt; } </code></pre> <p>That suggest that instead of returning a <code>Product</code>, you return a collection of <code>Category</code> <strong>with Products</strong>. Something like this in EF:</p> <pre><code>// I am typing it here directly // so I'm not sure if this is the correct syntax. // I assume you know how to do this, // anyway this should give you an idea. context.Categories.Include(o=&gt;o.Product) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload