2017-01-03 66 views
0

在此MVC5項目中,我希望我的「詳細信息」頁面也可用作編輯頁面和刪除頁面。MVC 5詳細信息頁面中的刪除操作

我正在通過創建2個表單來處理此任務,其中一個包含提交按鈕所需的所有內容。現在

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    @Html.HiddenFor(model => model.EmpresaID) 
    ... 
    <input type="submit" class="btn btn-primary" value="Submeter" /> 
} 

,爲我的第二個形式,我basicly有一個刪除按鈕:

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 
    @Html.HiddenFor(model => model.EmpresaID) 
    <input type="submit" class="btn btn-danger" value="Delete" /> 
} 

錯誤:

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)'

我試圖用ActionLink的,但後來我得到一個HTTP 404.這很奇怪,因爲我正在發送到正確的目的地:

@Html.ActionLink("Delete", "Delete", new { id = Model.EmpresaID }) 

發送到

.../Empresa/Delete/6 

EDIT1

操作方法:

// POST: Empresa/Delete/5 
     [HttpPost, ActionName("Delete")] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> DeleteConfirmed(int id) 
     { 
      Empresa empresa = await db.Empresas.FindAsync(id); 
      db.Empresas.Remove(empresa); 
      await db.SaveChangesAsync(); 
      return RedirectToAction("Index"); 
     } 

EDIT2

操作方法

// POST: Empresa/Delete/5 
[HttpPost] 
[ValidateAntiForgeryToken] 
public async Task<ActionResult> 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"); 
} 

Details.cshtml:

@using (Html.BeginForm("Delete", "Empresa", FormMethod.Post)) 
{ 
    @Html.AntiForgeryToken() 
    @Html.HiddenFor(model => model.EmpresaID) 
    @Html.ActionLink("Delete", "Delete", new { id = Model.EmpresaID }) 
    <button type="submit" class="btn btn-danger" value="Delete">Delete</button> 
} 

的ActionLink的不顯示任何錯誤,但它不會刪除任何東西。

該按鈕給了我一個HTTP 404.我在這裏做錯了什麼?

EDIT3

// POST: Empresa/Delete/5 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> 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"); 
    } 

與EDIT2唯一的問題是,我忘了保存更改。

它現在正常工作。

+1

請出示了操作方法的代碼。 – buffjape

+0

查看已更新的問題@buffjape – Lernas

+0

表單發佈之前或之後是否有404錯誤?您還需要在db.Empresas(e)'行後面保存對數據庫的更改,如'db.SaveChanges()'。再加上在這種情況下「e」是什麼? – Gareth

回答

2

您需要告知形式以及FormMethod(即GETPOST)的完成動作。因此,對於你比如刪除操作,這樣的事情:

@model MyProject.SomeViewModel 

@using (Html.BeginForm("Delete", "Empresa", FormMethod.Post)) 
{ 
    @Html.AntiForgeryToken() 
    @Html.HiddenFor(model => model.EmpresaId) 

    <button type="submit" class="btn btn-danger" value="Delete">Delete</button> 
} 

然後在你的控制器是這樣的:

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Delete(SomeViewModel viewModel) 
    { 
     //Do something with the posted viewModel 

     return RedirectToAction("Index"); 
    } 
+0

感謝您的輸入@Gareth,所以我改編了我的代碼,但仍然無法刪除。 – Lernas

+0

現在正在工作,我需要保存更改。等待db.SaveChangesAsync(); – Lernas