Search code examples
asp.net-mvcc#-4.0razorhtml.actionlink

To avoid auto generated Query string value in ActionLink


I was trying to add a css class to anchor but while rendering to html the href attribute has query string with value 4. The controller name "Home" length is appended to href link. If I remove the css class it's working fine. Ho to avoid the autogenerated query string value.

Razor Code:

@Html.ActionLink("Create Application", "CreateApplication", "Home", new {@class="link"});

Rendered html is :

<a href="/Account/CreateApplication?Length=4" class="link">Create Application</a>

Solution

  • If you want to pass the controller name as a parameter you can use the following signature:

    public static MvcHtmlString ActionLink(
        this HtmlHelper htmlHelper,
        string linkText,
        string actionName,
        string controllerName,
        Object routeValues,
        Object htmlAttributes
    )
    

    So, you should use it like this:

    @Html.ActionLink("Create Application", "CreateApplication", "Home", null, new { @class = "link" });