2017-01-03 122 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