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:
Code error
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With