Search code examples
asp.net-mvcasp.net-mvc-4html-helperactionlink

Create a custom ActionLink


I want to create a custom actionlink but I don't know how to do this while fulfilling my needs. I worked with custom htmlhelpers before but this is a bit more tricky for me.

The actionlink that I want to call needs to be like this:

@Html.CustomActionLink("LinkText", "Area","Controller","TabMenu","Action",routeValues, htmlAttributes)

so an example would be:

 @Html.CustomActionLink("Click here","Travel","Trip","Index","Index", new { par1 = "test", par2 = test2, new { @class = "font-color-blue" })`

Which would generate this html:

<a class="font-color-blue" href="/Trip/Travel/Index/Index?par1=test&par2=test2">Click Here</a>

And my route looks like:

 context.MapRoute(
            "EPloeg_default",
            "EPloeg/{controller}/{tabmenu}/{action}/{id}/{actionMethod}",
            new { action = "Index", id = UrlParameter.Optional, actionMethod = UrlParameter.Optional }
        );   

Any ideas how I can make this?


Solution

  • How about following code,

    @Html.ActionLink("Click here","Trip","Index", new { area= "Travel", tabmenu= "Index"}, new { @class = "font-color-blue" })
    

    EDIT

    You can use a extension method like this,

    public static MvcHtmlString CustomActionLink(this HtmlHelper htmlHelper, string linkText, string area, string controller, string tabMenu, string action, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
    {
        routeValues.Add("area", area);
        routeValues.Add("tabMenu", tabMenu);
        return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);
    }