title: MVC 5 Delete Action in Details Page
body: <p>In this MVC5 project, I want my Details page to also serve as Edit page and Delete page.</p>
  
  <p>I'm tackling this task by creating 2 forms, one with everything needed to update the data, including the submit button.</p>
  
  <pre><code>@using (Html.BeginForm())
  {
      @Html.AntiForgeryToken()
  
      @Html.HiddenFor(model =&gt; model.EmpresaID)
      ...
      &lt;input type="submit" class="btn btn-primary" value="Submeter" /&gt;
  }
  </code></pre>
  
  <p>Now, for my second form, I basicly have a Delete button:</p>
  
  <pre><code>@using (Html.BeginForm())
  {
      @Html.AntiForgeryToken()
      @Html.HiddenFor(model =&gt; model.EmpresaID)
      &lt;input type="submit" class="btn btn-danger" value="Delete" /&gt;
  }
  </code></pre>
  
  <p>Error:</p>
  
  <blockquote>
    <p>The parameters dictionary contains a null entry for parameter 'id' of
    non-nullable type 'System.Int32' for method
    'System.Threading.Tasks.Task`1[System.Web.Mvc.ActionResult]
    DeleteConfirmed(Int32)'</p>
  </blockquote>
  
  <p>I tried to use a ActionLink, but then I get a HTTP 404. Which is odd, since I am being sent to the correct destination:</p>
  
  <pre><code>@Html.ActionLink("Delete", "Delete", new { id = Model.EmpresaID })
  </code></pre>
  
  <p>Sends to</p>
  
  <pre><code>.../Empresa/Delete/6
  </code></pre>
  
  <p><strong>EDIT1</strong></p>
  
  <p>Action Method:</p>
  
  <pre><code>// POST: Empresa/Delete/5
          [HttpPost, ActionName("Delete")]
          [ValidateAntiForgeryToken]
          public async Task&lt;ActionResult&gt; DeleteConfirmed(int id)
          {
              Empresa empresa = await db.Empresas.FindAsync(id);
              db.Empresas.Remove(empresa);
              await db.SaveChangesAsync();
              return RedirectToAction("Index");
          }
  </code></pre>
  
  <p><strong>EDIT2</strong></p>
  
  <p>Action Method</p>
  
  <pre><code>// POST: Empresa/Delete/5
  [HttpPost]
  [ValidateAntiForgeryToken]
  public async Task&lt;ActionResult&gt; Delete([Bind(Include = "EmpresaID,Nome,Estado,Severidade,Inicio,Fim")] Empresa empresa)
  {
      //Do something with the posted viewModel
      Empresa e = await db.Empresas.FindAsync(empresa.EmpresaID);
  
      db.Empresas.Remove(e);
  
      return RedirectToAction("Index");
  }
  </code></pre>
  
  <p>Details.cshtml:</p>
  
  <pre><code>@using (Html.BeginForm("Delete", "Empresa", FormMethod.Post))
  {
      @Html.AntiForgeryToken()
      @Html.HiddenFor(model =&gt; model.EmpresaID)
      @Html.ActionLink("Delete", "Delete", new { id = Model.EmpresaID })
      &lt;button type="submit" class="btn btn-danger" value="Delete"&gt;Delete&lt;/button&gt;
  }
  </code></pre>
  
  <p>The ActionLink does not show any errors, but it doesn't delete anything either.</p>
  
  <p>The Button gives me a HTTP 404. What am I doing wrong here?</p>
  
  <p><strong>EDIT3</strong></p>
  
  <pre><code>    // POST: Empresa/Delete/5
      [HttpPost]
      [ValidateAntiForgeryToken]
      public async Task&lt;ActionResult&gt; Delete([Bind(Include = "EmpresaID,Nome,Estado,Severidade,Inicio,Fim")] Empresa empresa)
      {
          //Do something with the posted viewModel
          Empresa e = await db.Empresas.FindAsync(empresa.EmpresaID);
  
          db.Empresas.Remove(e);
          await db.SaveChangesAsync();
          return RedirectToAction("Index");
      }
  </code></pre>
  
  <p>The only problem with EDIT2 was that I forgot to save the changes.</p>
  
  <p>It's now working properly.</p>