【问题标题】:I can't to call action method in the controller from view我不能从视图中调用控制器中的操作方法
【发布时间】:2019-08-07 06:32:05
【问题描述】:

我想在“Index”视图提供的链接上从“Home”控制器调用“Delete Person”方法,但得到错误:“could not find this resource”:HTTP 404,URL:/Home /删除人/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


    【解决方案1】:

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

    【讨论】:

    • 这是我的 RoutConfig.cs - routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home" , action = "索引", id = UrlParameter.Optional });
    • 也许您配置了一个相对目录,这就是为什么您的以“/”开头的链接不起作用的原因。请使用 asp-* 属性来创建链接。如果这仍然不起作用,请向我们展示从浏览器的源代码查看器生成的链接。
    • 我已经从我的回答中删除了标签助手信息,它属于 ASP.NET Core,你仍在使用 ASP.NET MVC 5。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多