简体   繁体   中英

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/ ? Maybe this button is used wrong I'm new in mvc5 so be patient.

As documented here :-

2.An hyperlink or anchor tag that points to an action will ALWAYS be an HttpGet.

Try below code putting GET, as by default it takes [HttpGet] so remove [HttpPost] attribute

[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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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