Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to generate Html.ActionLink with icon

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.

like image 885
Yury Loginov Avatar asked Jan 30 '14 19:01

Yury Loginov


People also ask

What is 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 difference between HTML ActionLink and URL action?

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

How do I transfer my ActionLink model to my controller?

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.


2 Answers

Html.ActionLink() only supports plain-text links.

You should use <a href="@Url.Action(...)"> for more-complex links.

like image 70
SLaks Avatar answered Sep 28 '22 02:09

SLaks


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.

Edit

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.

like image 34
Trevor Nestman Avatar answered Sep 28 '22 01:09

Trevor Nestman