Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use an ActionLink containing an element?

As the question says, Is it possible to use an ActionLink containing an element, and if not, what is the best way to achieve it?

For example, lets say I have a Span element, I want the whole thing to be a hyperlink... The following works:

<a href="/Site/Page/@Model.ID?Type=test">
                <span class="box5">Click anywhere in this box</span> </a>

Imagine that span/css class box5 makes this large... Was originally a DIV, but, I found that didn't follow standards and this appears to follow ok.

This renders and works fine, but, is there anyway to use an ActionLink instead? I have tried to guess the syntax such as (copying from forms):

@using (Html.Actionlink){<span class="box5">Click anywhere in this box</span>}

and many other combinations without any luck.

Now, my manual HTML workaround works fine and I am happy to leave it, but, is it possible to use a MVC ActionLink, and if it is, should I even worry/would there be any benefits?

like image 635
Wil Avatar asked Nov 03 '11 06:11

Wil


People also ask

What is the use of HTML ActionLink?

Html. ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.

What is the difference between using URL action and HTML ActionLink in a view?

Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.

How can create href link in ASP NET MVC?

The Anchor Tag Helper generates HTML anchor (<a> </a>) element by adding a new attribute. The "href" attribute of the anchor tag is created by using new attributes. The Anchor Tag Helper generates an HTML anchor (<a> </a>) element by adding new attribute.


1 Answers

Just use @Url.Action instead:

<a href="@Url.Action("Page","Site", new { id = Model.Id, @Type = "test" })">
 <span class="box5">Click anywhere in this box</span> </a>
like image 173
Michael Stum Avatar answered Oct 16 '22 14:10

Michael Stum