To avoid auto generated Query string value in ActionLink

喜你入骨 提交于 2019-12-08 05:42:23

问题


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>

回答1:


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" });



回答2:


The signature you're calling is

ActionLink(string text, string actionName, object rotueValues, object htmlAttributes)

In particular, the third parameter is not a controller name, but an object of parameters to build the route from.

You can pass new { controller = "..." }



来源:https://stackoverflow.com/questions/17640536/to-avoid-auto-generated-query-string-value-in-actionlink

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!