在这个MVC5项目中,我希望我的详细信息页面也可以用作编辑页面和删除页面。
我正在通过创建2个表单来处理此任务,其中一个表单包含更新数据所需的所有内容,包括提交按钮。
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.EmpresaID)
...
<input type="submit" class="btn btn-primary" value="Submeter" />
}
现在,对于我的第二个表单,我基本上有一个删除按钮:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.EmpresaID)
<input type="submit" class="btn btn-danger" value="Delete" />
}
错误:
参数字典包含参数'id'的空条目 方法的非可空类型'System.Int32' “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不会显示任何错误,但也不会删除任何错误。
Button给了我一个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的唯一问题是我忘了保存更改。
它现在正常运作。
答案 0 :(得分:2)
您需要告知表单要完成的操作以及FormMethod
,即GET
或POST
。所以对于你的删除操作,例如,像这样:
@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");
}
答案 1 :(得分:0)
// EmpressasController
// POST: Empresas/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
var empresa = _db.Empresas.Find(id);
if (empresa != null) _db.Empresas.Remove(empresa);
_db.SaveChanges();
return RedirectToAction("Index");
}
// Razor View
@using (Html.BeginForm("Delete", "Empresas", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.EmpresaId)
<button type="submit" class="btn btn-danger">Delete</button>
}