重定向到操作 [HttpPost] 返回"resource not found"错误



我有一个相当简单的项目,即使用Ninject与ASP MVC 4和实体框架。我添加了一个基于视图的编辑并创建Action Result,没有问题。但是删除Action Result不起作用。

该视图是基于实体的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;
    }

错误是

找不到资源。

这很令人困惑,因为我觉得我不了解框架的非常基本的原则。据我了解,这意味着客户控制器没有相应的动作重点。但是还有。该教程正在通过建议删除操作应为 idempotent ,因此仅包含 [HTTPPPOST]

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>

尝试

@Html.ActionLink("Delete", "Delete_Client", null, new { id = item.ClientId })

您必须删除[httppost],这是get request

使用以下代码:

@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");
}

更改您的代码

@Html.ActionLink("Delete", "Delete_Client", new { item.ClientId }) 

to

@Html.ActionLink("Delete", "Delete_Client", new { id = item.ClientId })

最新更新