我有一个相当简单的项目是使用带有asp mvc 4和实体框架的ninject。 我添加了一个编辑并根据视图创建了ActionResult,没有任何问题。但是删除ActionResult无效。
视图是基于实体的IEnumrable,具有简单的ActionLink
@Html.ActionLink("Delete", "Delete_Client", new { item.ClientId })
控制器也非常简单。
[HttpPost]
public ActionResult Delete_Client(int id)
{
Client deleteClient = repository.DeleteClient(id);
if (deleteClient != null)
{
TempData["message"] = string.Format("{0} was deleted.", deleteClient.Name);
}
return RedirectToAction("Admin_Client_List");
}
这通过Iinterface
与模型交互 Client DeleteClient(int id);
并在实体框架中
public Client DeleteClient(int id)
{
Client dbEntry = context.Clients.Find(id);
if (dbEntry != null)
{
context.Clients.Remove(dbEntry);
context.SaveChanges();
}
return dbEntry;
}
错误是
无法找到资源。
这非常令人困惑,因为我觉得我不理解框架的一个非常基本的原则。据我所知,这意味着客户端控制器没有相应的ActionResult。但是还有。本教程正在进行中,建议删除操作应幂等,因此只包含 [HttpPost] 。
Uri看起来像这样
/Client/Delete_Client?ClientId=12
我认为可能需要看起来像这样
/Client/Delete_Client/12
然而,这不起作用。
更新了Admin_Client_List.cshtml的请求
@model IEnumerable<Project.Domain.Entities.Client>
@{
ViewBag.Title = "Client List";
ViewBag.Icon = "entypo-layout";
ViewBag.ClientActive = "active";
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
<a href="@Url.Action("Create_Client","Client")" class="btn btn-primary">Create a new Client</a>
<div class="row">
@foreach (var item in Model)
{
<div class="col-sm-3">
<div class="tile-progress tile-blue">
<div class="tile-header">
<a href="@Url.Action("Client_Details", "Client", new { id =item.ClientId})">
<h3>@item.Name <i class="entypo-right-open-big"></i> <span class="badge badge-secondary pull-right">7</span></h3>
</a>
</div>
<div class="tile-progressbar">
<span data-fill="78%" style="width: 78%;"></span>
</div>
<div class="tile-footer">
<h4>
<span class="pct-counter">78</span>% increase
</h4>
<span>@item.Description</span>
</div>
<div class="tile-header">
<a href="@Url.Action("Edit_Client", "Client", new { id = item.ClientId})" type="button" class="btn btn-blue btn-icon icon-left">
<i class="entypo-pencil"></i> Edit
</a>
@*<a href="javascript:$('#modal-6').modal('show', {backdrop: 'statuc'});" class="btn btn-default">Show Me</a>*@
@Html.ActionLink("Delete", "Delete_Client", new { id = item.ClientId })
@*<a href="@Url.Action("Delete_Client", "Client", new { item.ClientId })" class="btn btn-red btn-icon pull-right">Delete <i class="entypo-cancel"></i></a>*@
</div>
</div>
</div>
}
</div>
答案 0 :(得分:1)
使用以下代码:
@Html.ActionLink("Delete", "Delete_Client", new { id = item.ClientId })
[HttpPost]
public ActionResult Delete_Client(int id)
{
Client deleteClient = repository.DeleteClient(id);
if (deleteClient != null)
{
TempData["message"] = string.Format("{0} was deleted.", deleteClient.Name);
}
return RedirectToAction("Admin_Client_List");
}
答案 1 :(得分:1)
尝试
@Html.ActionLink("Delete", "Delete_Client", null, new { id = item.ClientId })
你必须删除[HttpPost],这是一个GET请求
答案 2 :(得分:0)
更改您的代码
@Html.ActionLink("Delete", "Delete_Client", new { item.ClientId })
到
@Html.ActionLink("Delete", "Delete_Client", new { id = item.ClientId })