简体   繁体   English

调用控制器动作MVC5

[英]Calling a controller action MVC5

Ok so I got action that is in controller and code is: 好的,所以我得到了控制器中的操作,代码是:

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

and in view I got 鉴于我

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

I got error 404 when I click button is possible to run this action without redirecting to url /SaveRow/ ? 单击按钮可以在不重定向到url /SaveRow/情况下运行此操作时出现错误404。 Maybe this button is used wrong I'm new in mvc5 so be patient. 也许此按钮使用错误我在mvc5中是新手,所以请耐心等待。

As documented here :- 作为记录在这里 : -

2.An hyperlink or anchor tag that points to an action will ALWAYS be an HttpGet. 2.指向动作的超链接或锚标记始终是HttpGet。

Try below code putting GET, as by default it takes [HttpGet] so remove [HttpPost] attribute 尝试在下面的代码中放入GET,因为默认情况下它需要[HttpGet]所以请删除[HttpPost]属性

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

and as an ideal design of MVC I would suggest you to use @Html.BeginForm to access pertiular POST call of edit functionailty 作为MVC的理想设计,我建议您使用@Html.BeginForm来访问edit functionailty的垂直POST调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM