Search code examples
asp.net-mvc-4razor-2

Create Link to level up controller using Razor in ASP.NET MVC4


This is my code that i'm using to create a link to top level link:

@Html.ActionLink("Edit",Url.Action("Edit","Home", new{row.Application_ID})) 
// supposed to return /Home/Edit/x (application_id)

Instead, it returns: /Home/Home/Edit/x // Current View is Home/Edit

Please help.

Edit 1: Route Table information:

//This is for Edit 
routes.MapRoute(
           "Edit", // Route name
           "Home/Edit/{application_id}", // URL with parameters
           new { controller = "Home", action = "Edit", application_id = UrlParameter.Optional } // Parameter defaults
        );

        // This is the default route, I've modified it to LogOn.
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Account", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
        );

Solution

  • As a small edition to haim770 answer. if you want for some reason use Url.Action instead Html.ActionLink you are always free to write it like that:

    <a href="@Url.Action("Edit", "Home", new { application_id  = row.Application_ID })">Edit</a>
    

    Some developers prefere this way because it more similar to plain Html, but result is the same as Html.ActionLink