Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET adding id to razor ActionLink [duplicate]

Tags:

asp.net-mvc

I need razor to generate a href links. This code works fine:

@Html.ActionLink("Questions", "Questions", "SectionPartials")

How do I set an id element to the a href tag? I already tried:

@Html.ActionLink("Questions", "Questions", "SectionPartials", new { id= "new-link" })

Which adds a id to the a href, but the navigations won't work anymore. This also gives a error in the code (but does compile tho)

Server error: enter image description here

Code error enter image description here

Coudn't find any more information on this issue.. If there is some information missing (like controller/view(?)), I am happy to add those!

Thanks

like image 616
Jim Vercoelen Avatar asked Jan 07 '23 02:01

Jim Vercoelen


1 Answers

The desired LinkExtensions.ActionLink method signature is:

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

So, your current new { id= "new-link" } is passed as the routeValues parameter. You need to pass it as the htmlAttributes instead:

@Html.ActionLink("Questions", "Questions", "SectionPartials", null, new { id= "new-link" });

Or (using named parameters):

@Html.ActionLink("Questions", "Questions", "SectionPartials", htmlAttributes: new { id= "new-link" });

See MSDN

like image 96
haim770 Avatar answered Jan 29 '23 14:01

haim770