I'm starting to learn ASP.NET MVC, and have a problem, how generate code with Html.ActionLink
like this:
<a href="~/Views/Home/Create.cshtml" class="btn btn-primary"> <i class="icon-pencil icon-white"></i> <span> <strong>Create</strong> </span> </a>
please.
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.
There is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.
ActionLink is rendered as an HTML Anchor Tag (HyperLink) and hence it produces a GET request to the Controller's Action method which cannot be used to send Model data (object). Hence in order to pass (send) Model data (object) from View to Controller using @Html.
Html.ActionLink()
only supports plain-text links.
You should use <a href="@Url.Action(...)">
for more-complex links.
I wanted to add to SLaks answer.
Using <a href="@Url.Action(...)">
with what user2567619 wanted.
<a href="@Url.Action("Create", "Home")" class="btn btn-primary"> <i class="icon-pencil icon-white"></i> <span> <strong>Create</strong> </span> </a>
I think it's worth mentioning that @Url.Action
can take it's parameters like this:
@Url.Action(string actionName, string controllerName)
Whereas @Html.ActionLink
can take it's parameters like this:
@Html.ActionLink(string linkText, string actionName, string controllerName)
It may be pretty obvious, but I thought it was worth noting.
As Peck_conyon noted, for both @Url.Action
and @Html.ActionLink
, these are just one of the ten different overload methods.
For documentation on UrlHelper.Action
, look here.
For documentation on LinkEtensions.ActionLink
, look here.
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