首页 > 解决方案 > 我不能从视图中调用控制器中的操作方法

问题描述

我想从“索引”视图提供的链接上的“主页”控制器调用“删除人员”方法,但得到错误:“找不到此资源”:HTTP 404,URL:/Home/DeletePerson/ 1. 我试过@Html.ActionLink,但它也不起作用。我的错误在哪里?该项目有 .NET Framework 4.7.2、Entity Framework 6.2.0、MVC 5。这个项目有 HomeController:

public class HomeController : Controller
{
    ...
    public ViewResult Index()
    {            
        ...
        return View("Index");
    }
    [HttpGet]
    public ActionResult DelelePerson(int id)
    {
       ...           
        return View(person);
    }
    [HttpPost]
    public ActionResult DeletePersonConfirmed(int id)
    {
        ...
        return RedirectToAction("Index");
    }
}

我的 Index.cshtml 包括

<td><p><a href="/Home/DeletePerson/@b.Id">Del</a></p></td>

标签: c#model-view-controller

解决方案


首先,使用Html.ActionLink创建正确的链接 他们知道你的具体路由配置。其次,您的链接可能看起来像“/Home/DeletePerson?id=@b.id”,但这取决于您尚未发布的路由配置。


推荐阅读