title: Error with C# in html working with models
body: <p>I created a repository and now i want to use a foreach to add multiple items to the page but its giving me a error. My objective is: when there is no items in the page it will show a message but if the page have items will show a different message and the items. The code is the following:</p>
  <p><strong>Controller:</strong></p>
  <pre><code>using Microsoft.AspNetCore.Mvc;
  using System.Collections.Generic;
  using Treino1.Models;
  
  namespace Treino1.Controllers
  {
      public class DespesasController : Controller
      {
          public IActionResult Index()
          {
              List&lt;Despesa&gt; despesas = RepositorioDespesas.Despesas;
              return View(despesas);
          }
  
  
          [HttpGet]
          public IActionResult NovaDespesa()
          {
              return View();
          }
  
          [HttpPost]
          public IActionResult NovaDespesa(Despesa despesa)
          {
              if (ModelState.IsValid)
              {
                  RepositorioDespesas.AddDespesa(despesa);
                  return View(&quot;DespesaConfirmada&quot;, despesa);
              }
                  return View();
          }
      }
  }
  </code></pre>
  <p><strong>Repository</strong></p>
  <pre><code>using System.Collections.Generic;
  
  namespace Treino1.Models
  {
      public static class RepositorioDespesas
      {
          private static List&lt;Despesa&gt; despesas = new List&lt;Despesa&gt;();
  
          public static List&lt;Despesa&gt; Despesas
          {
              get { return despesas; }
          }
  
          public static void AddDespesa (Despesa newDespesa)
          {
              despesas.Add (newDespesa);
          }
  
  
  
      }
  }
  
  
  
  @{
      ViewData[&quot;Title&quot;] = &quot;Despesa Confirmada&quot;;
  }
  
  @model List&lt;Treino1.Models.Despesa&gt;
  
  
  @if (Model.Count== 0)
  {
      &lt;h1&gt;Sem Despesas...&lt;/h1&gt;
  }
  else
  {
      &lt;h1&gt;Despesas!&lt;/h1&gt;
      @foreach (Treino1.Models.Despesa d in Model)
      {
          &lt;div class=&quot;card bg-secondary border-dark&quot;&gt;
              &lt;div class=&quot;card-body&quot;&gt;
                  &lt;b&gt;Nome da Despesa: &lt;/b&gt; @d.NomeDespesa
                  &lt;b&gt;Quantidade: &lt;/b&gt; @d.Quantidade
                  &lt;b&gt;Valor: &lt;/b&gt; @d.Valor
                  &lt;b&gt;Categoria: &lt;/b&gt; @d.Categoria
                  &lt;b&gt;Pago? &lt;/b&gt;
                  @if (d.Pago)
              {
               @:Sim
           }else
              {
                @:Não
              }
  
          &lt;/div&gt;
      &lt;/div&gt;
      
  }
  
  }
  
  &lt;div&gt;
      &lt;a asp-action=&quot;NovaDespesa&quot;&gt;Nova Despesa&lt;/a&gt;
  &lt;/div&gt;
  </code></pre>
  <p>I think the error is something about the Model.Count==0 but i dont know how to solve.</p>
  <p>The error is this:</p>
  <p><a href="https://i.stack.imgur.com/XX0A0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XX0A0.png" alt="enter image description here" /></a></p>