Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionLink htmlAttributes

WORKS

<a href="@Url.Action("edit", "markets", new { id = 1 })"              data-rel="dialog" data-transition="pop" data-icon="gear" class="ui-btn-right">Edit</a> 

DOES NOT WORK - WHY?

@Html.ActionLink("Edit", "edit", "markets", new { id = 1 }, new {@class="ui-btn-right", data-icon="gear"}) 

It seems you can't pass something like data-icon="gear" into htmlAttributes?

Suggestions?

like image 937
Pavel Hlobil Avatar asked Nov 05 '10 18:11

Pavel Hlobil


People also ask

What is ActionLink in C#?

ActionLink(HtmlHelper, String, String, Object, Object)Returns an anchor element (a element) for the specified link text, action, route values, and HTML attributes. C# Copy.

What is ActionLink in MVC?

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. Here are some samples of Html.

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.


1 Answers

The problem is that your anonymous object property data-icon has an invalid name. C# properties cannot have dashes in their names. There are two ways you can get around that:

Use an underscore instead of dash (MVC will automatically replace the underscore with a dash in the emitted HTML):

@Html.ActionLink("Edit", "edit", "markets",       new { id = 1 },       new {@class="ui-btn-right", data_icon="gear"}) 

Use the overload that takes in a dictionary:

@Html.ActionLink("Edit", "edit", "markets",       new { id = 1 },       new Dictionary<string, object> { { "class", "ui-btn-right" }, { "data-icon", "gear" } }); 
like image 60
marcind Avatar answered Oct 05 '22 11:10

marcind