C# 调用控制器操作MVC5

C# 调用控制器操作MVC5,c#,asp.net,asp.net-mvc-3,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc 3,Asp.net Mvc 5,好的,我得到了控制器中的动作,代码是: [HttpPost] public ActionResult SaveRow(int? id) { //some db operations. return RedirectToAction("Index"); } 在我看来 @Html.ActionLink(Translation.Accept, "SaveRow", new { id = item.New.RecId }) 单击按钮时出现错误404。是否可以在不重定向到url/Sav

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

[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新手,所以请耐心等待。

如文件所述:-

2.指向操作的超链接或锚定标记将始终是HttpGet

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

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

作为MVC的理想设计,我建议您使用
@Html.BeginForm
访问编辑功能的pertiular
POST
调用链接是
GET
,您已将该方法标记为
POST
。使用来访问它。使用[HttpGet]-认为您从代码中错过了这一点。默认情况下,需要[HttpGet]@RyanMcDonough始终值得添加,以便您可以一目了然,也可以覆盖默认值,因此如果发生这种情况,它仍然是GET。其intranet;)@GSerg@Neel我希望这会比我对ppl有更多的帮助;)