asp.net-mvcactionlink

receiving 404 error when trying to post to controller


I am trying to call into my controller method passing 2 parameters but the controller action is never hit and a 404 error is returned.

having looked at other similar questions I have tried reformatting the actionlink and also tried using @html.action, made sure that it is a HttpGet rather then a HttpPost and obviously made the action method is actually there in the controller.

Action result:

     @Html.ActionLink(
                   linkText: item.FileName,
                   actionName: "GetStatement",
                   controllerName: "Statements",
                   routeValues: new { id = item.Id, entityCode = 
    item.EntityCode },
                   htmlAttributes: null)

Controller method

public class StatementsController : Controller
    {
        [HttpGet]
        public ActionResult GetStatement(int id, int entityCode)
        {
           //go to repository and get statement
        }
    }

I am also not sure that the corresponding URL is formatted correctly: Statments/GetStatement/1234?entityCode=111


Solution

  • Please check this one you need to change some small pieces of code.

    In cshtml page

     @Html.ActionLink(
                       linkText: item.FileName,
                       actionName: "GetStatement",
                       controllerName: "Statements",
                       routeValues: new { itemid = item.Id, entityCode = 
        item.EntityCode },
                       htmlAttributes: null)
    

    Controller code

    public class StatementsController : Controller
        {
            [HttpGet]
            public ActionResult GetStatement(int itemid, int entityCode)
            {
               //go to repository and get statement
            }
        }
    

    Note: when you pass "Id" in controller action then automatically route convert in below example

    public ActionResult HandleException(int id)
            {
                // id mentioned in **RouteConfig** file that's way URL automatic mapped
            }
    

    See the RouteConfig file

    public class RouteConfig
        {
            public static void RegisterRoutes(RouteCollection routes)
            {                
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
                routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );
            }            
        }
    

    Image