【问题标题】:Calling a controller action MVC5调用控制器动作 MVC5
【发布时间】:2014-10-01 08:19:49
【问题描述】:

好的,所以我得到了控制器中的操作,代码是:

[HttpPost]
public ActionResult SaveRow(int? id)
{
    //some db operations.
    return RedirectToAction("Index");
}

我看到了

@Html.ActionLink(Translation.Accept, "SaveRow", new { id = item.New.RecId })

单击按钮时出现错误 404 是否可以在不重定向到 url /SaveRow/ 的情况下运行此操作? 也许这个按钮用错了我是mvc5的新手,所以请耐心等待。

【问题讨论】:

  • 链接为GET,您已将方法标记为POST。使用a form 到达它。

标签: c# asp.net asp.net-mvc-3 asp.net-mvc-5


【解决方案1】:

据记录 here :-

2.指向动作的超链接或锚标记始终是 HttpGet。

试试下面的 GET 代码,默认情况下它需要 [HttpGet] 所以删除 [HttpPost] 属性

[HttpGet]
public ActionResult SaveRow(int? id)
{
    //some db operations.
    return RedirectToAction("Index");
}

作为 MVC 的理想设计,我建议您使用 @Html.BeginForm 访问 pertiular POST 调用编辑功能

【讨论】:

  • 使用 [HttpGet] - 认为你错过了代码。
  • 默认需要 [HttpGet] @RyanMcDonough
  • 总是值得添加,以便您一目了然,您也可以覆盖默认值,因此如果发生这种情况,它仍然是 GET。
  • 它的内网 ;) @GSerg
  • @Neel Thx 我希望这将帮助更多的人然后我;)
猜你喜欢
  • 2014-11-28
  • 2014-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-23
  • 2010-09-29
  • 1970-01-01
相关资源
最近更新 更多